http://qs1969.pair.com?node_id=825877

seekperlwisdom has asked for the wisdom of the Perl Monks concerning the following question:

more simplified version of my question

[% totalprice = 900 %]<---- 1 -----> [% FOREACH shopping %] [% modelname = key %][% information = value %] [% MAKE_NAME = information.MAKE %] <a href="/cgi-bin/welcome.pl?....>some link</a><br/> Quantity: [% information.QTY %] x [% information.PRICE %]<br/> [% ff = information.PRICE * information.QTY %] PRICE = [% ff %]<br/># lets say it is equal to 100 [% totalprice = totalprice + ff %] <---- 2 ------> [% totalprice %]#print out 1000 <hr/> [% END %] [% totalprice %]<----- 3 --------> #print out 900 </div>

<-- 2 --> get updated but <-- 3 --> still get 900, what i really trying to achieve that <--2--> value to pass to <--3-->. thanks

Replies are listed 'Best First'.
Re: Template toolkit Adding Values to Variable
by CountZero (Bishop) on Mar 01, 2010 at 12:22 UTC
    Could it be that there is something wrong with the Perl-script that stuffs data in the TT-variables?

    Have you checked what is in information.QTY and information.PRICE? Or perhaps shopping is zero or empty or undef and the loop doesn't run?

    Does TT know the += operator (as in [% totalprice += ff %]? Have you tried [% totalprice = totalprice + ff %]? (Sorry, I cannot check on the computer I am writing this on).

    Update: Added question on += operator

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Thanks for reply,

      everything runs perfect except for the updating the [% totalprice %] variable. seem like [% totalprice   %] is reset or become private varible when it is inside the [% FOREACH %]. it is just creating global variable which can update or value can be pushed in it.

      regards.
Re: Template toolkit Adding Values to Variable
by merlyn (Sage) on Mar 01, 2010 at 18:21 UTC
    [% totalprice + ff %] <---- 2 ------>
    That doesn't change the value of totalprice. Did you mean GET totalprice = totalprice + ff perhaps?

    -- Randal L. Schwartz, Perl hacker

    The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

      Its my mistake i am changing it right now. i didnt write the proper code when i was makig simplified version to make my question more clear. but in fact it is [% totalprice = totalprice + ff %]. it seems like as i answered to " CountZero " , 'totalprice' variable become private when it is inside the

      [% FOREACH %]
      block because i printed the value inside the block and it does change but as soon it comes out of the block it retain the initial value ( the one before the [% FOREACH %]). i have tried to use [% BLOCK PROCESS %] but it still does not make any differnce. i might need to get the value from the script.

      But in your answer,dont you think it should be 'SET' what i have red in the ' Perl Template Toolkit ' book.

      thanks
        I deliberately used GET because I want to execute the calculation and interpolate it into the text. Without GET, you're chosing one or the other.

        -- Randal L. Schwartz, Perl hacker

        The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

Re: Template toolkit Adding Values to Variable
by ahmad (Hermit) on Mar 01, 2010 at 12:31 UTC

    I am not sure if what you wrote would actually work or not, To my knowledge ... Template Toolkit foreach loops should be written as  [% FOREACH x IN  shopping %] Then you can use x.key to do what you want.

    As I know Template Toolkit doesn't accept +=

      Code runs fine. it depend on the variable coming from the script. It is hash which i am using, if it is array then your given method is applied.

      thanks

        Where did you get this idea from?

        it works with a hash just like it works with arrays

        Anyway I have tried it and it works fine for me.

        [% totalprice = 900 %] [% FOREACH x IN shopping %] [% modelname = x.key %][% information = x.value %] [% MAKE_NAME = information.MAKE %] Quantity: [% information.QTY %] x [% information.PRICE %]<br/> [% ff = information.PRICE * information.QTY %] PRICE = [% ff %]<br/> [% totalprice = totalprice + ff %] [% totalprice %]#print out 1000 <hr/> [% END %] [% totalprice %]<----- 3 -------->

        It prints out 1000 as the final number not 900

consider separating your business logic from your presentation code
by metaperl (Curate) on Mar 05, 2010 at 19:06 UTC