Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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’,]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 %]] 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading