[Templates] Referring to variables defined outside BLOCKs

Paul Makepeace tt2.org@paulm.com
Tue, 19 Nov 2002 01:22:33 +0000


The subject sounds complicated but really I just want a variable
referred to in a block that wasn't defined there to be long lived. For
example,

  [% total = 0 %]
  [% MACRO add(c) BLOCK %]
   <li>Total := [% total %] + [% c %] = [% total = total + c; total %].</li>
  [% END %]

Here'd I'd like $total to count up. Right now it doesn't because of the
copying that goes on for top level variables (as explained in
T::M::Variables in "Local v. Global" AIUI). To me this is a bit weird,
like everything's perl-ishly 'local'.

A hack seems to be something like,

  [% BLOCK add %]
   <li>Total := [% total %] + [% c %] = [% total = total + c; total %].</li>
  [% END %]

  <ul>
  [% add(3) %]            [%# total lost each call %]
  [% add(2) %]
  [% PROCESS add c = 3 %] [%# total preserved each call %]
  [% PROCESS add c = 2 %]
  </ul>

(gives,
    * Total := 0 + 3 = 3.
    * Total := 0 + 2 = 2.
    * Total := 0 + 3 = 3.
    * Total := 3 + 2 = 5.
)

..or I suppose to leverage the lack of deep copy by doing something like
my.total everywhere it's needed. What's the general opinion here?

So is there some way to call a MACRO as though it's PROCESSED?

As ever, pointers to where I missed this in the ::Manual welcome :-)

Thanks,
Paul

-- 
Paul Makepeace ....................................... http://paulm.com/

"If drawing is like riding a bike, then it would be easy to lose
 weight."
   -- http://paulm.com/toys/surrealism/