in reply to Re^2: Templating suggestions for code generation
in thread Templating suggestions for code generation
With loop.first and loop.last (which I sometimes wish Perl had), you can simplify this even further by getting rid of the outer "if":// BEGIN [% IF list -%] switch(x) { [% FOREACH item IN list -%] case [% item %]: // code for [% item %] [% UNLESS loop.last -%] break; [% END -%] [% END -%] } [% END -%] // END
The loop won't be run if there are no elements, so you don't need to test if elements exist or not. Then, once in the loop, you put "ahead of first time" and "after last time" things inside the loop.first and loop.last conditionals. Nice pattern. In the abstract:[% FOREACH item IN list -%] [% IF loop.first -%] // BEGIN switch(x) { [% END %] case [% item %]: // code for [% item %] [% UNLESS loop.last -%] break; [% ELSE -%] } // END [% END -%]
[% FOR item IN list %] [% IF loop.first %] ... before first item .. [% END %] ... each item ... [% UNLESS loop.last %] ... between items ... [% ELSE %] ... after last item ... [% END; END %]
-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.
|
|---|