Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

HTML::Template's watchful eye

by jeyroz (Monk)
on Jun 24, 2005 at 18:32 UTC ( [id://469785]=perlquestion: print w/replies, xml ) Need Help??

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

My web applications (in most cases) use a main "HTML::Template" driven template for the structure of the page and include smaller templates for each screen. For example, my main_template.html will contain the header, main content divs, error / success divs, footer, etc. In order to build a search screen or a results screen I <TMPL_INCLUDE> a smaller specific template to build out the display ... simple. My question is this:

If I break the <TMPL_INCLUDE> path of an included template, HTML::Template complains (as it should). But, if this template isn't actually being called for display in that specific run mode (ie: it's hidden behind a <TMPL_IF>), why is H::T checking its path? This tells me that H::T is checking all <TMPL_INCLUDE> paths whether they are being used or not. While I am happy that H::T is looking out for my "best-interests", this seems a little wasteful if only a single template is being used out of a possible X number of templates.

Any thoughts? Am I missing something?

Thanks, as always, for your input.

author => jeyroz

Replies are listed 'Best First'.
Re: HTML::Template's watchful eye
by davidrw (Prior) on Jun 24, 2005 at 19:20 UTC
    It must be the case that the TMPL_INCLUDE processing is done first, before it tries to evaluate the template to generate output, which is when it processes the TMPL_IF's .. This is consistent with the HTML::Template docs that say "The included template contents are used exactly as if its contents were physically included in the master template." which i guess implies that the source include must exist.

    I don't know if there's a HTML::Template equivalent (don't see one at a glance), but in Template Toolkit you can do:
    [% TRY %] [% INCLUDE some_file.tpl %] [% CATCH %] <!-- some_file.tpl didn't exist --> [% END %]
Re: HTML::Template's watchful eye
by Xaositect (Friar) on Jun 24, 2005 at 19:38 UTC

    I'm guessing the easiest work-around would be to add to your template include path so that even templates that don't actually get used can be found, like:

    my $template = HTML::Template->new( filename => 'file.tmpl', path => [ '/path/to/templates', '/alternate/path' ] );

    I've become increasingly unhappy with HTML::Template lately, largely due to the inability to do a construct like

    <tmpl_include name="<tmpl_var name='foo'>">
    which is another form of what you seem to be looking for. As far as I know, the only way to accomplish something like this is to use the "filter" option, like:
    my $tmpl = HTML::Template->new( 'filename' => "foo.tmpl", 'filter' => sub { my $text_ref = shift; $$text_ref =~ s/special include tag/the actual include file ta +g/; } );


    Xaositect - Whitepages.com
      I'd never considered this as a problem, as I accomplish the same thing with a construct like:
      my $master = HTML::Template->new(filename=>'master.tmpl'); ### BLAH, BLAH ### my $sub_tmpl = HTML::Template->new(filename=>$foo); $sub_tmpl->param('variable'=>'value'); $master->param('include_foo' => $sub_tmpl->output); __END__ The template like: <tmpl_var name='include_foo' default=''>
      So far I haven't encountered a case where this approach isn't just peachy, but of course YMMV.

      Yoda would agree with Perl design: there is no try{}

        Ooooh... That hadn't occured to me. I like it. ++!


        Xaositect - Whitepages.com
        A reply falls below the community's threshold of quality. You may see it by logging in.

      Well I certainly love the simplicity of H::T but I agree with your comment. Your example was one of my very first frustrations.

      Other than my rather insignificant gripes, I enjoy using the tool.

      author => jeyroz

Re: HTML::Template's watchful eye
by izut (Chaplain) on Jun 24, 2005 at 19:21 UTC
    I think it is the correct behavior. I think HTML::Template first compiles your template to some Perl code, and then executes it. If it can't find some include, open() will fail.

    surrender to perl. your code, your rules.

      Close. You're thinking of something more like Text::ScriptTemplate which compiles to perl code, then executes it.

      HTML::Template does "compile" the template, but to an internal data format which it then can save (to disk, shared memory, whatever). Since this phase doesn't look at the passed-in variables, it doesn't know that the if clause is false. It very well could be true next time, and next time we may not even look at the templates because we'll reuse the template that is pre-compiled, all for speed.

      And that's the overwhelming idea with HTML::Template - speed. It has many options on caching, just to try to get the right amount of speed from your system.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://469785]
Approved by injunjoel
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-09-15 09:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (21 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.