Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Dynamic TMPL_INCLUDE in HTML::Template

by gryphon (Abbot)
on May 03, 2002 at 16:52 UTC ( [id://163843]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings fellow monks,

I've been using HTML::Template in combination with CGI::Application for a while now with much success and joy. However, I've recently come into a situation where I need some advice. I have a series of templates being used by a Web application which use HTML::Template's TMPL_INCLUDE tag to insert additional templates. I'm quickly running into a situation where I'm going to need to define the name of the included template in the Perl code of application module, not the template tag. For example, here's what I have right now:

<TMPL_INCLUDE name="page_header_type_1.tmpl"> <P>Welcome to this page, <TMPL_VAR name=firstname>.</P> <TMPL_INCLUDE name="page_footer_type_1.tmpl">

And of course, the Perl code inside the CGI::Application-type screen handler subroutine looks something like this:

my $self = shift; my $tmpl_obj = $self->load_tmpl('generic_page.tmpl', die_on_bad_params => 1, filter => \&tmpl_filter); $tmpl_obj->param( firstname => 'Bob' ); return $tmpl_obj->output;

The problem is that now I need to have several "page_handler_type_*.tmpl" files, and I'll only know which one to include at run-time. What I'd really love to do is embed via TMPL_VAR the name of the to be included template file for the header inside the TMPL_INCLUDE tag, much like how you can embed TMPL_VAR tags inside any other HTML tag:

<TMPL_INCLUDE name="<TMPL_VAR name="includeheadername">">

Then I could just add "includeheadername" to the list of stuff sent to $tmpl_obj->param(), but that's not allowed. I could also construct the final page as page parts generated by multiple "$tmpl_obj"s inside the screen handler subroutine, but that starts to defeat the whole purpose of using HTML::Template in the first place.

Any thoughts, suggestions, comments about how best to solve this dilema?

-gryphon
code('Perl') || die;

Replies are listed 'Best First'.
Re: Dynamic TMPL_INCLUDE in HTML::Template
by samtregar (Abbot) on May 03, 2002 at 18:58 UTC
    This can be eaily accomplished using the filter option to HTML::Template->new(). For example, in your template:
    <tmpl_include magic>
    Then in your code:
    my $filename = "magic_include.tmpl"; my $filter = sub {$$_[0] =~ s/<tmpl_include magic>/<tmpl_include $f +ilename>/}; my $t = HTML::Template->new(filename => "magic.tmpl", filter => $filter);
    By replacing the special <tmpl_include> with a real one using a generate template name you will achieve a dynamic template include.

    I really should put this in the FAQ!

    -sam

Re: Dynamic TMPL_INCLUDE in HTML::Template
by vladb (Vicar) on May 03, 2002 at 18:10 UTC
    I'm wondering if you could process a template twice to achieve what you want. For example, on the first pass, HTML::Template will parse this code:
    <TMPL_VAR name="include_link">
    by passing  include_link => qq/<TMPL_INCLUDE name="$custom_link">/ to the param() method of your template object. So, considering for example that your $custom_link is set to 'foo_page.html', the initial template should be parsed into this:
    <TMPL_INCLUDE name="foo_page.html">
    Then, you simply have to use this parsed output to initialize yet another template object and 'reparse' it the last time to get exactly what you want.

    I'll try to build a little sample script for you to look at in the mean time ;-)

    UPDATE: here's some sample code implementing what I was just talking about:

    main script:
    use strict; use HTML::Template; my $tmpl = new HTML::Template( filename => 'first.tmpl' ); # generate custom page link... my $custom_page = "second.tmpl"; # inject a custom piece of Template code $tmpl->param(special_page_include => qq/<TMPL_INCLUDE name = "$custom_ +page">/); # process custom built template.. my $tmpl_final = new HTML::Template(scalarref => \$tmpl->output()); print $tmpl_final->output();
    first.tmpl
    Including custom page... <TMPL_VAR name = "special_page_include"> ----------- rest of the page
    second.tmpl
    SECOND PAGE!
    script output (test run):
    Including custom page... SECOND PAGE! ----------- rest of the page


    "There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-19 08:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found