The reason this doesn't work in HTML::Template is because TMPL_INCLUDEs are handled in the first parsing phase, and TMPL_VAR's and every other TMPL_ tag are handled in the second parsing phase.

The first phase parses the template and checks to make sure all tags are balanced and consistent, and also reads in and merges any TMPL_INCLUDEs leaving one big template. The results are then optionally cached in case they are needed for a second run.

By the time the second phase runs, which does the actual variable substitutions, there is no record of what was an include, everything looks like one big template.

There is an alternate (very simple) way that you could handle it, using simple TMPL_IF conditionals:

Stuff here <TMPL_IF NAME="NEED_INCLUDE_1"> <TMPL_INCLUDE NAME="INCLUDE_1"> </TMPL_IF> <TMPL_IF NAME="NEED_INCLUDE_2"> <TMPL_INCLUDE NAME="INCLUDE_2"> </TMPL_IF> - or with HTML::Template::Expr - <TMPL_IF EXPR="DYNAMIC_INCLUDE eq 'include_file_1.tmpl'"> <TMPL_INCLUDE NAME="include_file_1.tmpl"> </TMPL_IF> Stuff here

But keep in mind that every one of the includes will be read in and parsed in the first phase, so it can be expensive if you have a lot of dynamic includes. Using the caching system will alleviate that somewhat. It is also made somewhat more awkward since HTML::Template doesn't have an ELSIF tag.

Your best bet is probably to go with the 2 Template object method explained by another poster...

- Cees


In reply to Re: HTML::Template .... misfeature? by cees
in thread HTML::Template .... misfeature? by dragonchild

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.