Dynamic CSS/JS/HTML in Template Toolkit wrappers
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.
You might also enjoy:
« Previous Article: Debugging long-running perl programs in Debian
Next Article: Phantom Traffic Jams »
- David
- Kristian


