I've been trying to find some real-world examples of how HTML::Template excels over CGI in terms of outputting lots of repetitive HTML constructs. There are those who claim HTML::Template is better, but I can't find any actual reasons or explanations to back that up. It seems to be based more on personal opinion, than anything else. In my case, I have a portal site I've written, which has LOTS of HTML driving it, and I want to make sure I can easily maintain it, as I continue to add functionality and features.

Here's one example:

Using CGI.pm, I have the following construct:

my @menubar = ( {title =>'home', width =>'10', height =>'9', param =>'home', alt =>'Return to the front page'}, {title =>'donate', width =>'15', height =>'9', param =>'donate', alt =>'Send a donation to help'}, # {...} and so on, 9 menu options ); $cgi->start_div({-id=>'mline'}), $cgi->end_div(), "\n\n", $cgi->start_div({-id=>'tmbox'}); print join " | ", map { $cgi->span({-class => 'mitem'}, $cgi->a({-href => "$script?a=$_->{param}", -title => "$_->{alt}"}, $cgi->img({-src => "i/$_->{param}.png", -border => '0', -width => "$_->{width}", -height => "$_->{height}", -alt => "($_->{alt})"}), " $_->{title} "),),} @menubar; print $cgi->end_div();

Nothing magical, and it prints out those menu options, with the graphic, alt tag, href, and a separator "|" bar between, except after the last one, for visual aquity (thanks bart for the suggestion here).

Now, to do this in HTML::Template, I do the following:

my $template = HTML::Template->new( filename => 'menubar.tmpl'); $template->param(MENUBAR => [ {title =>'home', width =>'10', height =>'9', param =>'home', alt =>'Return to the front page'}, {title =>'donate', width =>'15', height =>'9', param =>'donate', alt =>'Send a donation to help'}, # {...} and so on, 9 menu options ] ); print $template->output(); # Yes, still using CGI.pm here for right now $cgi->start_div({-id=>'mline'}), $cgi->end_div(), "\n\n", $cgi->start_div({-id=>'tmbox'});

And the template for this looks like:

<TMPL_LOOP NAME=BOTTOM_LIST> <span class="mitem"> <a title="<TMPL_VAR NAME=TITLE>" href="?a=<TMPL_VAR NAME=PARAM>"> <img height="<TMPL_VAR NAME=HEIGHT>" border="0" src="i/<TMPL_VAR NAME=PARAM>.png" alt="<TMPL_VAR NAME=ALT>" width="<TMPL_VAR NAME=WIDTH>" /> &nbsp;<TMPL_VAR NAME=PARAM>&nbsp; </a> </span> </TMPL_LOOP>

It basically outputs the same thing, but what I don't understand is... why add the complexity of a separate HTML template (which is much more complicated to maintain and update, and substantially larger in terms of lines-of-code) versus just doing it all in CGI.pm?

What's the draw? What is the benefit of switching over? Is there really any benefit? I can see delegating the HTML portion of template development to web-monkeys, and the code to the developers themselves, but in this case, I am cook, bottle-washer, and captain of this ship, so it exponentially complicates my maintenance with more files to update, more lines of code to manage, and in general, complexity with no perceived added-benefit of using it.

Lastly, are there any other modules that can handle the same/similar sort of construct (dynamic generation of options from array refs, etc.). I've looked briefly into CGI::Application, Text::Template, the horror that is Embperl, and Template Toolkit, and wonder if learning the additional metal-languages and other constructs is really worth the pain. My goal is to refactor the number of static subs of HTML I have into something more modular, manageable, and bite-sized.


In reply to HTML::Template vs. CGI.pm by hacker

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.