I am contemplating building a code generator system, but I'm not sure on how to implement the code templates. I am preoccupied with simplicity of the syntax for the templates themselves (not necessarily for the associated perl code), to achieve some typical tasks.

So, this is a bit of a contest for templating systems, I am curious how you would implement one particular example problem, in your favourite templating system. The example problem is this: starting with a Perl array, it should produce the following output:

empty array: nothing is inserted
// BEGIN // END
one or more items: a global block is inserted (in this example, a switch statement, with braces), and for each item a subblock is inserted inside that global block (the individual cases). With the array = ("foo", "bar", "baz"), the desired output is:
// BEGIN switch(x) { case foo: // code for foo break; case bar: // code for bar break; case baz: // code for baz } // END

I'd like to point out one particularity, which often appears to be a weak spot. Note that there is no "break;" line after the last item in the list. The idea is that some particular text should be inserted between items in a loop, but not after the last item. It should act like the join string in join. As another example, it should be possible to expand a Perl array ("foo", "bar", "baz") into the text

(foo, bar, baz)
Thus, with commas between the items, but not at the end.

Again: at this point, I'm only interested in the templates themselves, and not yet in the Perl code to drive it.

To show what I am after, it could be something like the following, fictional example:

// BEGIN <%IF @LIST%> switch(x) { <%FOREACH @LIST AS ITEM%> case $ITEM: // code for $ITEM <%CONTINUE%> break; <%/FOREACH%> } <%/IF%> // END

So, if you know of a templating system that can do something like this: great! If you know of something better still... even better! :)


In reply to Templating suggestions for code generation by bart

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.