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>" /> <TMPL_VAR NAME=PARAM> </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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |