When you're using HTML::Template and want to put a table into a page, you have two options: You can embed loop logic in the template, or you can inject a table constructed elsewhere into the template. If you favor reuse, you're going to lean towards injection. But that doesn't mean you can't use templates for both parts. Here's a variation that does both, in a reusable way.

A simple embedded table looks like

... stuff ... <table> <TMPL_LOOP rows> <tr> <TMPL_LOOP columns> <td><TMPL_VAR ESCAPE=HTML value></td> </TMPL_LOOP> </tr> </TMPL_LOOP> </table> ... more stuff ...

and the code to expand it looks like

my $template = HTML::Template->new(rows => ...); print $template->output;

You can isolate part of the above into a separate template, and pull it in to the main template via

... stuff ... <TMPL_INCLUDE NAME="table.tmpl"> ... more stuff ...

This expands the included template in-line, with no visible difference to the calling program, though you do get the benefit of having a reusable table template. But you can do better, as we'll see below.

When injecting a table, the template looks like

... stuff ... <TMPL_VAR ESCAPE=0 table> ... more stuff ...

You're then on the hook for providing a chunk of HTML to supply as the template parameter table. You can construct the HTML from code, from a heredoc, by using CGI.pm tag support, or by some other means. Whichever way you go, you'll end up doing something like

my $template = HTML::Template->new(filename => 'bigpage.tmpl'); my $tableHTML = tableHTML(...); $template->param(table => $tableHTML); print $template->output;

A possibly non-obvious "other means" is to produce the table HTML by using a separate HTML::Template instance that uses its own template for the table part. This looks like

sub tableHTML { my %table_data = @_; my $template = HTML::Template->new(filename => 'table.tmpl'); $template->param(%table_data); return $template->output; }

You now have the basis for a reusable component that you can pull out any time you need to embed tables in a template. Variations include passing a query into the component, which then extracts data from some data source. This technique also extends nicely to other types of components.


In reply to Reusable template components with HTML::Template by dws

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.