Dynamic CSS/JS/HTML in Template Toolkit wrappers

A Tech article with View Comments posted 19 December 2006.
Tags: ,

I’ve been asked about this a few times on #tt, so I thought I’d provide it for future Googlers. If you’re looking to optionally include certain files in a Template Toolkit wrapper, it’s very easy. You just provide the file names as a parameter to the WRAPPER call.

First, the wrapper file itself, wrapper.tt:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>[% title %]</title>
<body>
[% IF includes %]
    <div id="sidebar">
    [% FOREACH include_file = includes %]
        [% INCLUDE $include_file %]
    [% END %]
    </div>
    <div id="content">
[% ELSE %]
    <div id="wide_content">
[% END %]

[% content %]</div>

</body>
</html>

And then you call the file like this:

[% WRAPPER wrapper.tt
    title = 'My Title'
    includes = [
        'includedfile1.tt',
        'includedfile2.tt'
    ]
%]

<h1>Content for middle</h1>

[% END %]

It’s pretty straightforward to adapt this to CSS or JS files, or whatever you need to customize in the wrapper.

View Comments to “Dynamic CSS/JS/HTML in Template Toolkit wrappers”

  1. Kristian says:

    As a “future Googler”, thank you.

  2. David says:

    Another “future Googler” checking in! Thanks for the guide.

Leave a Reply

blog comments powered by Disqus