Re: [template] please help me in template tool kit module

[email protected] (Randal L. Schwartz)
Newsgroups gmane.comp.lang.perl.modules.template-toolkit
Message-ID <[email protected]>
>>>>> "Ronald" == Ronald J Kimball <rkimball-jkI2qz8QNLsmlAP/[email protected]> writes:

Ronald> You should be outputing only the current address each time through the
Ronald> loop.  Here's an example:

Ronald> [%- mylist = [ 'one' ] %]
Ronald> [%- mylist = [ 'one', 'two' ] %]
Ronald> [%- mylist = [ 'one', 'two', 'three' ] %]
Ronald> [%- FOREACH item IN mylist %]
Ronald>   [%- IF loop.max() == 0 -%]
Ronald>     [%- item %].
Ronald>   [%- ELSIF loop.last() -%]
Ronald>     [%- IF loop.max() > 1 %]or [% END %][% item %].
Ronald>   [%- ELSE %][% item %], [% END %]
Ronald> [%- END %]

Ronald> I put one of the [% END %]s at the end of the line to preserve the trailing
Ronald> space character in the output.  Adjust the style to your liking.

I find this a little clearer:

$ tpage
[%
BLOCK listem;
    FOR item IN items;
      item;
      ", " IF loop.count < loop.max;
      " or " IF loop.count == loop.max;
    END; # FOR
END; # BLOCK

FOR try = [0..5];
  try; ": <";
  INCLUDE listem items=[1..try];
  ">\n";
END; # try
%]
^D
0: <>
1: <1>
2: <1 or 2>
3: <1, 2 or 3>
4: <1, 2, 3 or 4>
5: <1, 2, 3, 4 or 5>
$


-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[email protected]> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.