Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Dynamic includes in Template::Toolkit

by Molt (Chaplain)
on May 07, 2003 at 10: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?

Comment on Dynamic includes in Template::Toolkit
Select or Download Code
Re: Dynamic includes in Template::Toolkit
by Molt (Chaplain) on May 07, 2003 at 11: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.

Re: Dynamic includes in Template::Toolkit
by zby (Vicar) on May 07, 2003 at 11: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 (Priest) on May 07, 2003 at 11: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.

    Cheers
    Ben.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2014-09-24 03:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (244 votes), past polls