I'm currently working on a web application using Template Toolkit, but now I'm experiencing difficulties trying to work out the nicest way to allow dynamic includes.

What I'm trying to do is have a template including other templates dynamically based on either the status of a variable, or the result of a function, so that the following could be made to work. Please note I do know that the 'header' and 'body' in the includes are taken as literals, it's this I'm trying to get round here.

As part of program:

$template->process( 'toplevel.html', { title => 'Hello, World', header => 'current_header.html', body => 'welcome_page.html' } };

As toplevel.html

<html> <head> <title>[% title %]</title> </head> <body> [% INCLUDE header %] <!-- To include current_header.html, somehow--> [% INCLUDE body %] <!-- To include welcome_page.html --> </body> </html>

The aim of this is to allow me to associate new actions to page contents without a change either to the basic code, or to the top-level page templates.

I could code up a plugin to do this, but fear reinventing wheels. Any hints?


In reply to Dynamic includes in Template::Toolkit by Molt

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.