hacker has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: HTML::Template vs. CGI.pm
by dragonchild (Archbishop) on Nov 10, 2003 at 20:14 UTC | |
by perrin (Chancellor) on Nov 10, 2003 at 20:47 UTC | |
by LTjake (Prior) on Nov 10, 2003 at 20:38 UTC | |
|
Re: HTML::Template vs. CGI.pm
by jeffa (Bishop) on Nov 10, 2003 at 20:43 UTC | |
|
Re: HTML::Template vs. CGI.pm
by samtregar (Abbot) on Nov 10, 2003 at 20:16 UTC | |
|
Re: HTML::Template vs. CGI.pm
by jdtoronto (Prior) on Nov 10, 2003 at 20:18 UTC | |
|
Re: HTML::Template vs. CGI.pm
by hardburn (Abbot) on Nov 10, 2003 at 21:28 UTC | |
|
Re: HTML::Template vs. CGI.pm
by Zed_Lopez (Chaplain) on Nov 10, 2003 at 22:43 UTC | |
|
Re: HTML::Template vs. CGI.pm
by argggh (Monk) on Nov 10, 2003 at 21:13 UTC | |
by perrin (Chancellor) on Nov 11, 2003 at 00:40 UTC | |
by argggh (Monk) on Nov 11, 2003 at 12:14 UTC | |
by perrin (Chancellor) on Nov 11, 2003 at 16:18 UTC | |
|
Re: HTML::Template vs. CGI.pm
by Purdy (Hermit) on Nov 11, 2003 at 16:20 UTC | |
|
Re: HTML::Template vs. CGI.pm
by thraxil (Prior) on Nov 11, 2003 at 17:02 UTC |