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;


In reply to Dynamic TMPL_INCLUDE in HTML::Template by gryphon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.