Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Dynamic includes in Template::Toolkit

by Molt (Chaplain)
on May 07, 2003 at 06:58 UTC ( #256162=perlquestion: print w/replies, xml ) Need Help??

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

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?

Replies are listed 'Best First'.
Re: Dynamic includes in Template::Toolkit
by zby (Vicar) on May 07, 2003 at 07:34 UTC
    Perhaps I've misunderstood something - but it seems fairly simple - just use a variable ($header). From TT manual (Directives):
    [cut] The INCLUDE directive automatically quotes the filename specified, as +per INSERT described above. When a variable contains the name of the +template for the INCLUDE directive, it should be explicitly prefixed +by '$' or double-quoted [% myheader = 'my/misc/header' %] [% INCLUDE myheader %] # 'myheader' [% INCLUDE $myheader %] # 'my/misc/header' [% INCLUDE "$myheader" %] # 'my/misc/header'

      No, this is exactly what I was trying to do. It's been a while since I used tt2 so just went to the normal POD rather than the manual.

      Thank you very much for this, saves me an awful lot of switch statements.

Re: Dynamic includes in Template::Toolkit
by benn (Vicar) on May 07, 2003 at 07:48 UTC
    the Directives Manual # Include says that you can do this...
    [% INCLUDE $myheader %]
    I haven't tried it myself, but it therefore looks like what you want to do should be possible.


Re: Dynamic includes in Template::Toolkit
by Molt (Chaplain) on May 07, 2003 at 07:07 UTC

    Couple of typing mistakes in my question.. 'get roung' should read 'get round', and 'I code code up a plugin' should be 'I could code up a plugin'.

    Thanks to broquaint for pointing out the latter. I thought I'd better post to correct that one since it makes the last bit look like utter nonsense.

    Update: Ah, didn't know broquaint could remove these mistakes. Thanks to him for doing that. Move along now, nothing to see here.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://256162]
Front-paged by broquaint
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2023-12-08 06:09 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (35 votes). Check out past polls.