http://qs321.pair.com?node_id=635789

hacker has asked for the wisdom of the Perl Monks concerning the following question:

I'm gutting and rewriting a website I wrote about 7 years ago that used static CGI.pm calls to output HTML, and it got quite lengthy, quite fast (try putting a long FAQ into CGI.pm's $cgi->p($cgi->a({-href=>' ...'})); syntax all over the place, every sub gets to be hundreds of lines long, ugh).

So I started rerolling it into HTML::Template, and I'm wondering what the best approach is for the workflow. What I have looks like this:

$cgi = CGI->new(); $action = $cgi->param('a'); my %dispatch = ( home => sub { \&home(); }, donate => sub { \&donate(); }, news => sub { \&news(); }, faq => sub { \&faq(); }, dl => sub { \&download(); }, bugs => sub { \&bugs(); }, # ... and so on ); if (defined $action) { $action =~ s/[^A-Za-z0-9 ]*/home/g; } &begin_page(); $action = HTML::Entities::encode($action); $action = $cgi->param('a') && $dispatch{$cgi->param('a')} || $dispatch{'home'}; $action->(); &end_page(); sub home { my $template = HTML::Template->new( die_on_bad_params => '0', filename => 'home.tmpl'); my $content = $template->output; print $content; }

In this context, every sub prints some HTML which is pulled from a series of templates.

Here's where it gets tricky:

Since I can't put a template inside a template (or at least, when I try to do that, HTML::Template just prints the name of the second template into the body of the first template, instead of the second template's contents), I am using File::Slurp to slurp some static HTML from files into scalars, and pushing that into my template, like this:

my $template = HTML::Template->new( die_on_bad_params => '0', filename => 'news.tmpl'); my $latest_news = read_file('latest_news'); $template->param(LATEST_NEWS => $latest_news); my $content = $template->output; print $content;

Inside the template called 'news.tmpl', I have:

<TMPL_VAR NAME=LATEST_NEWS>

Basically I have "static" HTML which I want to just read in and print out, but inside the middle of that static HTML file, I need to insert dynamic bits of HTML (for example, query the database for latest news, wrap some HTML around the query results and stuff that in the middle of the template).

This gets a bit redundant and confusing fast, since each sub from my dispatch, is basically going to be doing the same thing:

sub home { my $template = HTML::Template->new( die_on_bad_params => '0', filename => 'home.tmpl'); } sub donate { my $template = HTML::Template->new( die_on_bad_params => '0', filename => 'donate.tmpl'); } sub news { my $template = HTML::Template->new( die_on_bad_params => '0', filename => 'news.tmpl'); } sub faq { my $template = HTML::Template->new( die_on_bad_params => '0', filename => 'faq.tmpl'); }

...and so on.

What I'm wondering, is... there MUST be a better, cleaner way to do this, while reducing the amount of "duplicity" here in each sub.

Ideas?