template toolkit remove , from the last entry

I have the following seciton on the template toolkit

names: [[% FOREACH name IN name %] '[% name %]', [% END %]] 

which prints something like this :

names : [‘name1′,’name2′,’name3’,]

my question is there an option to tweak the above in order not to print the last comma i.e the result will be

names : [‘name1′,’name2′,’name3’]

>Solution :

Inside every FOREACH loop, you have access to the special loop variable – which is an instance of the Template::Iterator class.

So you can write something like this:

names: [[% FOREACH name IN name %] '[% name %]'[% UNLESS loop.last %],[% END %] [% END %]] 

Leave a Reply