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
| [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
Re: Template toolkit Adding Values to Variable
by merlyn (Sage) on Mar 01, 2010 at 18:21 UTC
|
| [reply] [d/l] [select] |
|
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 | [reply] [d/l] [select] |
|
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.
| [reply] [d/l] |
|
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 +=
| [reply] [d/l] |
|
| [reply] |
|
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
| [reply] [d/l] |
|
consider separating your business logic from your presentation code
by metaperl (Curate) on Mar 05, 2010 at 19:06 UTC
|
| [reply] |