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?


In reply to Help with Template Workflow 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.