Your example does not really make a good case for templating. Simple variable substitution is not hard to do without templates. You run into trouble however once you try to dynamically generate tables, lists or such of unpredetermined size.

In that case I find that inlining HTML actively promotes bad programming habits.

I've seen far too often code where little bits of the output are produced by prints strewn throughout various loops and subroutiness here and there and everywhere. Needless to say one change to the layout that requires tags to be changed in concert between several such parts may require hours of scrambling around.

But just a simple change of look isn't even the biggest problem. The real problem with such a style is that you cannot change the alignment of your output without jumping through major hoops. That is, if you had a 2-column vertical output, and you want to change it to a 2-row horizontal output f.ex, you have to change the entire code's logic around to accomodate the new style. UGH. Don't go there.

With templates on the other hand, what your code does is collect all the data into a well-defined data structure, and then hands it all over to the templating mechanism. Even if there are major changes to be made to the way the output is produced, your code's logic never needs touching.

That's why you should use templates. I find that I now use templates for even the simplest of CGIs if the markup I'm going to produce isn't completely static.

____________
Makeshifts last the longest.

In reply to Re: OUTPUT: inline or template_based? by Aristotle
in thread OUTPUT: inline or template_based? by CodeHound

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.