Updating a variable, in a TPL file?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Arthur Dent

    Updating a variable, in a TPL file?

    Hello all, sorry for the cross-post, but im not sure which group is best for
    this question.

    I am an ASP.NET developer, but am learning PHP/perl for the first time now
    to make some to changes to a client's site which was done by someone else.

    I wrote a debug TPL which ive got working great now and was Such a sense of
    accomplishment! ! :) . People always say how cryptic C can be, but i think
    perl definitely beats C out! Anyway.... In the
    /classes/com/<site>/runtime/Page.class.php i have some code which reads
    whether the user has requested debug mode and presets some variables for the
    debug.tpl i wrote. Here is the code i have in my Page.class file:

    $smarty->assign("wantde bug","0");
    if(isset($_REQU EST['debug'])){
    $smarty->assign("wantde bug","1");

    $smarty->assign("sesobj ",$_SESSION );
    < above line repeated for $_PUT, $_GET and $_COOKIE >
    }

    The following code is the code of my debug.tpl. Everything from the opening
    B tag to the final BR tag is repeated over for the $_PUT, $_GET and $_COOKIE
    smarty variables set up in the Page.class.

    {if $wantdebug}
    <div style="backgrou nd-color: white; border: solid 1px black;
    padding=3px;">
    <b style="cursor: hand;"
    onclick="javasc ript:document.a ll['lblSession'].style.display= (document.all['lblSession'].style.display= ='none')?'':'no ne';">&lt;sessi on&gt;</b>
    <label id='lblSession' style="display: none;"><br>
    {foreach from="$sesobj" key="key" item="value"}
    &nbsp;&nbsp;&nb sp;&nbsp;{$key} =={$value|repla ce:"<br>":"|"}< br>
    {foreachelse}
    &nbsp;&nbsp;&nb sp;&nbsp;no session items<br>
    {/foreach}
    <b>&lt;/session&gt;</b>
    </label><br>
    </div>
    {/if}

    This all works fine and dandy, and the "<session>" text in the B tag acts as
    a nice little toggle switch to hide show the array contents, like the way
    the little +/- signs do in IE when viewing a raw XML file.

    Now here comes my problem.... I want to include the TPL at both the top and
    the bottom of my pages, so that i can see the contents of all the variables
    before and after the page has done all its processing to see how things have
    been affected. But now when i refer to document.all['lblSession'] there are
    two of them on the page. So what i want to do is to have a variable which i
    can append to the label tag's id attribute to make unique ids, such as
    "lblSession 1" and "lblSession 2". How can i do this? I though i could create
    a variable in the Page.class code, then use it in the TPL something like
    id='lblSession{ $MyID}' . Then update the variable in each inclusion of
    the header so each time through the label's ID would be unique based on this
    variable, but i cant seem to get the variable to update. What i tried are
    the couple following things, but none worked.....
    any help how to do this? Thanks in advance!!!

    1st tried:
    <label id='lblSession{ $MyID++}' style="display: none;">

    2nd tried:
    {if $wantdebug}
    ..... code omitted, from block included above ............... .

    {php}
    $MyID++
    {/php}
    {/if}

    3rd tried:
    {if $wantdebug}
    ..... code omitted, from block included above ............... .

    {php}
    $smarty->assign("MyID", $MyID+1)
    {/php}
    {/if}


  • Robin

    #2
    Re: Updating a variable, in a TPL file?

    Hi,

    this seems to be a Smarty-related problem, I guess, since e.g.

    print "<label id='lblSession{ $MyID}'>";

    $MyID++;

    print "<label id='lblSession{ $MyID}'>";

    works.

    Sad but true, I don't have any experiences with Smarty, but what about a
    {$MyID++}? Like

    {if $wantdebug}
    ..... code omitted, from block included above ............... .

    {$MyID++}

    ...
    {/if}



    Arthur Dent wrote:[color=blue]
    > Hello all, sorry for the cross-post, but im not sure which group is best for
    > this question.
    >
    > I am an ASP.NET developer, but am learning PHP/perl for the first time now
    > to make some to changes to a client's site which was done by someone else.
    >
    > I wrote a debug TPL which ive got working great now and was Such a sense of
    > accomplishment! ! :) . People always say how cryptic C can be, but i think
    > perl definitely beats C out! Anyway.... In the
    > /classes/com/<site>/runtime/Page.class.php i have some code which reads
    > whether the user has requested debug mode and presets some variables for the
    > debug.tpl i wrote. Here is the code i have in my Page.class file:
    >
    > $smarty->assign("wantde bug","0");
    > if(isset($_REQU EST['debug'])){
    > $smarty->assign("wantde bug","1");
    >
    > $smarty->assign("sesobj ",$_SESSION );
    > < above line repeated for $_PUT, $_GET and $_COOKIE >
    > }
    >
    > The following code is the code of my debug.tpl. Everything from the opening
    > B tag to the final BR tag is repeated over for the $_PUT, $_GET and $_COOKIE
    > smarty variables set up in the Page.class.
    >
    > {if $wantdebug}
    > <div style="backgrou nd-color: white; border: solid 1px black;
    > padding=3px;">
    > <b style="cursor: hand;"
    > onclick="javasc ript:document.a ll['lblSession'].style.display= (document.all['lblSession'].style.display= ='none')?'':'no ne';">&lt;sessi on&gt;</b>
    > <label id='lblSession' style="display: none;"><br>
    > {foreach from="$sesobj" key="key" item="value"}
    > &nbsp;&nbsp;&nb sp;&nbsp;{$key} =={$value|repla ce:"<br>":"|"}< br>
    > {foreachelse}
    > &nbsp;&nbsp;&nb sp;&nbsp;no session items<br>
    > {/foreach}
    > <b>&lt;/session&gt;</b>
    > </label><br>
    > </div>
    > {/if}
    >
    > This all works fine and dandy, and the "<session>" text in the B tag acts as
    > a nice little toggle switch to hide show the array contents, like the way
    > the little +/- signs do in IE when viewing a raw XML file.
    >
    > Now here comes my problem.... I want to include the TPL at both the top and
    > the bottom of my pages, so that i can see the contents of all the variables
    > before and after the page has done all its processing to see how things have
    > been affected. But now when i refer to document.all['lblSession'] there are
    > two of them on the page. So what i want to do is to have a variable which i
    > can append to the label tag's id attribute to make unique ids, such as
    > "lblSession 1" and "lblSession 2". How can i do this? I though i could create
    > a variable in the Page.class code, then use it in the TPL something like
    > id='lblSession{ $MyID}' . Then update the variable in each inclusion of
    > the header so each time through the label's ID would be unique based on this
    > variable, but i cant seem to get the variable to update. What i tried are
    > the couple following things, but none worked.....
    > any help how to do this? Thanks in advance!!!
    >
    > 1st tried:
    > <label id='lblSession{ $MyID++}' style="display: none;">
    >
    > 2nd tried:
    > {if $wantdebug}
    > ..... code omitted, from block included above ............... .
    >
    > {php}
    > $MyID++
    > {/php}
    > {/if}
    >
    > 3rd tried:
    > {if $wantdebug}
    > ..... code omitted, from block included above ............... .
    >
    > {php}
    > $smarty->assign("MyID", $MyID+1)
    > {/php}
    > {/if}
    >
    >[/color]

    Comment

    Working...