kappa has asked for the wisdom of the Perl Monks concerning the following question:

Hello, fellow monks. I seek for your collective wisdom :)

The task at hands is simple. I have a page which includes several nearly identical html chunks (read as follows: a form with several groups of widgets for date input). I'd like (naturally) to <TMPL_INCLUDE> them, but I don't seem to be able by design. Those widgets must have different names.

The way I'll probably do it now:

my $tmpl = new HTML::Template filename => 'form.tmpl.html'; my $date = new HTML::Template filename => 'dateinput.tmpl.html'; $date->param(ctrl_name => 'start'); $tmpl->param(start_date => $date->output); $date->param(ctrl_name => 'end'); $tmpl->param(end_date => $date->output);
And I'd like to just:
<TMPL_INCLUDE NAME='dateinput.tmpl.html' ctrl_name=start> ... <TMPL_INCLUDE NAME='dateinput.tmpl.html' ctrl_name=end>

Both ways are awkward and the second is also impossible with current HTML::Template.

Any suggestions?

Replies are listed 'Best First'.
Re: HTML::Template generic includes
by matija (Priest) on Mar 29, 2004 at 15:24 UTC
    The second method isn't impossible. You could define a filter subroutine (as a parameter to HTML::Template::new) that would handle the includes, and modify the content accordingly. (In fact, it could use HTML::Template do do it.)
      Ahh, I see. With the ability to filter I can just implement a special tag for the case and let that <TMPL_INCLUDE> alone. Thanks for quite a good suggestion!
Re: HTML::Template generic includes
by amw1 (Friar) on Mar 29, 2004 at 15:13 UTC
    I don't know how invested you are in HTML::Template, if you're pretty heavy into it than you can probabbly ignore the rest of this post. Fyi I think the only real way to do this with HTML::TEmplate is the first method you specified.
    Stuff like this is one of the main reasons I ended up switching from HTML::Template to Template::Toolkit. With template toolkit you can do something like
    [% # THis is in templatelib.tt %] [% MACRO date_template(parm1, parm2, parm3) BLOCK %] [% SWITCH parm1 %] [% CASE "value 1" %] display this way [% CASE "value 2" %] display other way [% CASE DEFAULT %] display default way [% END %] [% END %]
    Your main template could look something like
    [% # this is foo.tt %] [% PROCESS templatelib.tt %] [% date_template(parm_from_script, "foo", "bar") %]
    your perl would look something like
    my $template = Template->new(); my $t_vars = { parm_from_script => "value 1", other_stuff => "mmmm donuts", }; $template->process("foo.tt", $t_vars) || die $template->error();
    Granted, there is now "code" inside the html, but it's still all display logic. It's not the processing logic. I tend to make the line between perl/templates in that perl will handle anything that's not strictly for display while the templates will handle everything else. People disagree with this and they're right too. :P