kellan <[EMAIL PROTECTED]> writes:

> Is there a tt2 idiom for "commafying" a list of items?
> 
> I'm generating a link of items with the code like:
> 
> [% FOREACH item = list %]
>   item.NAME,
> [% END %]

You could write [% list.join(', ') %]

> in a template language i was using, they had a construct called SEPARATOR
> so you had:
> 
> [% REPEAT item = list %]
>   item.NAME
>   [% SEPARATOR %],[% END SEPARATOR %]
> [% END REPEAT %]
> 
> so that the results of the loop were joined with a comma.

You could also say:

[% FOREACH item = list %]
   $item.NAME
   [% ',' UNLESS loop.last %]
[% END %]


Or more verbose:

[% FOREACH item = list %]
   $item.NAME
   [% UNLESS loop.last %]
      ,
   [% END %]
[% END %]


Or saving yourself from all that markup:

[%
   FOREACH item = list;
      item.NAME;
      ',' UNLESS loop.last;
   END;
%]


-- 
/ Jonas  -  http://jonas.liljegren.org/myself/en/index.html


Reply via email to