Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-20 06:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found