Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

HTML::Template macros

by blokhead (Monsignor)
on Dec 04, 2003 at 23:17 UTC ( #312367=snippet: print w/ replies, xml ) Need Help??

Description: With the loop_context_vars switch in HTML::Template you can make some useful macros. I found myself needing things like this all too often:
$tmpl->param( foos => \@foo, num_foos => scalar @foo, bars => \@bar, num_bars => scalar @bar, ... );
So I made a macro that handles this inside the template by adding a <TMPL_NUM foo> directive. Does anyone else have useful HTML::Template tricks up their sleeves?

By the way, I'm well aware that this is a great example of the serious limits of templating using HTML::Template's philosophy. This is some ugly template-ese under the hood. This is why I will probably switch over to Template::Toolkit in the not-so-distant future (at least for projects where I'm the one writing the template).

use HTML::Template;

sub tmpl_num_filter {
    s[<TMPL_NUM (\w+)>]
     [<TMPL_IF $1><TMPL_LOOP $1><TMPL_IF __LAST__><TMPL_VAR __COUNTER_
+_></TMPL_IF></TMPL_LOOP><TMPL_ELSE>0</TMPL_IF>]g
        for ${+shift};
}

my $tmpl = HTML::Template->new(
               scalarref         => \do { local $/; <DATA> },
               filter            => \&tmpl_num_filter,
               die_on_bad_params => 0,
               loop_context_vars => 1
           );

$tmpl->param(hobbits => [
    { name => "Frodo" },
    { name => "Samwise" },
    { name => "Meriadoc" }
]);
print $tmpl->output;

__DATA__
These are my <TMPL_NUM hobbits> favorite hobbits:
<ul><TMPL_LOOP hobbits>
  <li><TMPL_VAR name>
</TMPL_LOOP></ul>

Comment on HTML::Template macros
Download Code
Replies are listed 'Best First'.
Re: HTML::Template macros
by jeffa (Bishop) on Dec 05, 2003 at 04:14 UTC
    ++blokhead, but if you find yourself needing more of those "things", you might want to look into Template. Now don't get me wrong, i love HTML::Template, but sometimes i like having more expressional power, and Template has it:
    These are my [% hobbits.size %] favorite hobbits: <ul> [% FOREACH hobbit = hobbits %] <li>[% hobbit.name %]</li> [% END %] </ul>
    Another cool thing (possibly overkill) is that each hobbit could be a real object, not just a hash in a list.

    Oh yeah, you can get rid of the file slurp in your code by using the filehandle attribute instead of the scalarref one:

    filehandle => \*DATA,
    Much nicer and more efficient. Cheers. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Much nicer and more efficient. Cheers. :)
      It's just as efficient, but not more efficient (look before you speak ) :D
Re: HTML::Template macros
by jZed (Prior) on Dec 05, 2003 at 00:02 UTC
    Here's a hack to set the value of variables directly in a template itself.
    use HTML::Template; my $tmpl = HTML::Template->new( scalarref => \do { local $/; <DATA> }, die_on_bad_params => 0, loop_context_vars => 1, filter => sub { my $s=shift; $$s=~s/%~([^~]+)~%/<TMPL_VAR NAME="$1">/g; }, ); for my $key($tmpl->param) { next unless $key =~ /^SET_VAR_([^_]+)_(.+)$/i; $tmpl->param($1=>$2); } print $tmpl->output; __DATA__ %~SET_VAR_p1_http://somelong.net/path~% %~SET_VAR_p2_http://someother.net/otherpath~% %~p1~%/foo.html %~p1~%/bar.html %~p2~%/baz.html %~p2~%/qux.html
Re: HTML::Template macros
by thraxil (Prior) on Dec 05, 2003 at 04:51 UTC

    for populating <select>'s i use this sub:

    sub selectify { my $values = shift; my $labels = shift; my $selected = shift; my %selected = map {$_ => 1} @{$selected}; return [map { { value => $_, label => shift @{$labels}, selected => $selected{$_} || "", } } @{$values}]; }

    you use it in a script like:

    $template->param(foo_loop => selectify(\@values,\@labels,\@selected));

    and a template file like:

    <select name="foo"> <tmpl_loop name="foo_loop"> <option value="<tmpl_var name="value">"<tmpl_if name="selected"> sel +ected="selected"></tmpl_if>> <tmpl_var name="label"> </option> </tmpl_loop> </select>

    i usually have the stuff inside the <tmpl_loop> factored out into its own file so i can just do a <tmpl_include> anytime i have a select list. ie, this is what i you actually see all through my templates:

    <select name="foo"> <tmpl_loop name="foo_loop"> <tmpl_include name="option.tmpl"> </tmpl_loop> </select>

    update: fixed some unescaped entities.

Re: HTML::Template macros
by jZed (Prior) on Dec 04, 2003 at 23:41 UTC

    I like to use simpler tags for variables in templates so I use the filter to turn %~foo~% into <TMPL_VAR NAME="foo"> like so:

    use HTML::Template; my $tmpl = HTML::Template->new( scalarref => \do { local $/; <DATA> }, die_on_bad_params => 0, loop_context_vars => 1, filter => sub { my $s=shift; $$s=~s/%~([^~]+)~%/<TMPL_VAR NAME="$1">/g; }, ); $tmpl->param( userid => 99999 ); $tmpl->param( username => 'foo' ); print $tmpl->output; __DATA__ <a href="%~userid~%">%~username~%</a> is easier to edit than <a href="<TMPL_VAR NAME="userid">"><TMPL_VAR NAME="username"></a>
    The heck with patents, here's how to protect your thoughts: <tinfoil>.o0(Martians can't read this)</tinfoil>
Re: HTML::Template macros
by samtregar (Abbot) on Dec 05, 2003 at 18:30 UTC
    Interesting hack! I'd probably do this by overriding param() in a class that inherits from HTML::Template. Then just have param() autogenerate num_foo for each loop foo.

    -sam

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://312367]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2015-08-01 03:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (285 votes), past polls