You generally will build the entire data structure ahead of time, but you don't have to. Another documented option - the
associate parameter to the new() method, allows you to associate any object that provides a param() method to the given template. Then, what
HTML::Template will do when it needs a parameter (for a TMPL_VAR, TMPL_LOOP, or TMPL_IF/UNLESS), is check the following places in order:
- Its parameter stash
- The associated object(s), in the order they were associated in
- If within a loop and GLOBAL_VARS has been specified, it will check the next outermost scope, repeating the same actions
The param() method is expected to return
undef if it doesn't handle that parameter name. (Make sure you specify the case_sensitive option or be case-insensitive yourself.)
So, you can now do something like:
package My::Associated::DBH::Wrapper;
# Called as:
# my $thingy = My::Associated::DBH::Wrapper->new(
# sth => $sth,
# param_name => 'Foo',
# );
sub new {
my $class = shift;
my %args = @_;
bless \%args, $class;
}
sub param {
my $self = shift;
my ($param) = @_;
return unless $param eq $self->{param_name};
my @values = $self->{sth}->fetchrow_array;
return unless @values;
return $values[0];
}
Then, when you build your template, you can go ahead and do this:
my $template = HTML::Template->new(
filename => 'my_template.tmpl',
associate => [ $thingy, $query ],
);
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.