Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Two questions about HTML::Template

by Cody Pendant (Prior)
on Jun 23, 2006 at 10:55 UTC ( [id://557138]=perlquestion: print w/replies, xml ) Need Help??

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

I'm working on a site where a URL calls a different template depending on where it's been called from.

For instance if the script gets called at myserver.com/foo/ then the script parses the path and uses the template "foo.tmpl". If it's in myserver.com/bar/ it uses the template "bar.tmpl" and so on.

I'd like to go one further -- I'd like "foo.tmpl" to include a file: <tmpl_include name="foo.inc"> and "bar.tmpl" to include a different file <tmpl_include name="bar.inc">.

Is there any way to do this? The obvious way to do it is something like, create a variable $foonav and pass it to the template and have

<tmpl_if name="foonav"> <tmpl_include name="foo-nav.inc"> </tmpl_if>

And so on for a bunch of other variables. But that feels pretty kludgy.

And which brings me to my second question, why, when I do this:

<tmpl_if name="foonav"> <tmpl_include name="foo-nav.inc"> </tmpl_if> <tmpl_if name="barnav"> <tmpl_include name="bar-nav.inc"> </tmpl_if>
Does it cause the following error message: HTML::Template->new() : Cannot open included file /path/to/foo-nav.inc : file not found. at /usr/share/perl/5.8/HTML/Template.pm line 2241.

Even when I haven't ever called on "bar-nav.inc"? No matter whether the variable is passed to grab the include, I get the error. I'm busy developing and haven't created the file yet. It somehow seems inefficient for HTML::Template to be checking the existence of files when it doesn't need to. Can I tell it not to?



($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
=~y~b-v~a-z~s; print

Replies are listed 'Best First'.
Re: Two questions about HTML::Template
by tinita (Parson) on Jun 23, 2006 at 11:35 UTC
    maybe you want dynamic includes? if the suggestion of wfsp does not work for you, you could try HTML::Template::Compiled:
    <tmpl_include_var name="var_with_path"> and feed the template filename through param().

    edit 2006/07/04: in the upcoming version of H::T::C there's the possibility to use lazy-loading. this will parse and execute templates only when they are really needed.

Re: Two questions about HTML::Template
by wfsp (Abbot) on Jun 23, 2006 at 11:29 UTC
    Perhaps you could could consider having foo.tmpl and bar.tmpl in their own dirs. Each dir could have it's own foot_nav.inc. You could lose that if completely (always a good idea!). From the docs: "the path to the enclosing file is tried first".

Re: Two questions about HTML::Template
by rhesa (Vicar) on Jun 23, 2006 at 13:17 UTC
    The reason for the error is that tmpl_include tags are interpreted before anything else. The tmpl_ifs around it don't stop HTML::Template from including the inner template, they would only hide the resulting output. In other words, there is no such thing as dynamic includes in HTML::Template.
Re: Two questions about HTML::Template
by cees (Curate) on Jun 23, 2006 at 15:31 UTC

    in HTML::Template, the includes are processed when the template is parsed, and the variables and if statements are interpolated when the template is processed. So your includes all have to exist, and they will all be loaded and parsed regardless of any if statements around them.

    When you want to change the template that is included based on a decision in the code, I often find it easier to change the include path that HTML::Template uses, and then use the same include file name in your template. By changing the include path. HTML::Template will look in a different place for the include file when the template is parsed. Here is some psuedo code to illustrate it:

    my @path; if ($foo) { push @path, '/foo'; } else { push @path, '/bar'; } push @path, '/standard_templates'; my $template = HTML::Template->new( filename => 'file.tmpl', path => \@path; );
      That makes the most sense to me, I'll use that, thanks cees.


      ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
      =~y~b-v~a-z~s; print

      I use CGI::Application and for the edification of like-minded folks, here's what I came up with (basically an array of paths, the 2nd one used for site-specific tmpls):

      #instance script passes in PARAMS which is also the directory with sit +e specific .tmpls my $site = $self->param('name_of_site'); $self->param('project' => '/usr/home/somedomain/ecomm/'); $self->param('tmpl_path' => [ $self->param('project_').'tmpls/', $self->param('project_path').'tmpls/ +includes/'.$self->param('site') ]);


      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-18 14:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found