create new tag
, view all tags
Warning: Alpha Code, interfaces may change radically. Do not use in production code.

Template Toolkit Plugin

This is a rather slim glue to Andy Wardley's Template Toolkit (see http://www.template-toolkit.org/ or TWiki:Codev.TemplateToolkit for more, though the latter is pretty stale).

I myself have here and then argued that TT is too heavyweight for use as a template system for TWiki, but this does only prevent using TT as the only template system for TWiki.

If, on the other hand, you do have TT installed anyway, and especially if you have it running in a mod_perl environment where module compilation time is no longer an issue, then you might want to share your TT blocks or layouts with TWiki.

The implementation as a plugin does not allow to install TT as a drop-in replacement for TWiki's own templating system. But I think that for first experiments it is possible to emulate that: Use very slim TWiki templates to get TWiki's template system out of the way, and run your topics through TT instead.

To be honest, I am using this plugin as both a first step to evaluate TT as a TWiki template machine, and as a real-life hands-on example for BenchmarkContrib to play with.

Syntax Rules

The plugin has just one TWiki tag in the TWiki sense:

  • Syntax example: %TEMPLATETOOLKIT{"on" WRAPPER="myTTtemplate.tt2"}%
  • If present in a topic with a value of "on", the whole topic will be processed by TT.
  • A configuration variable named $TWiki::cfg{Plugins}{TemplateToolkitPlugin}{UseTT} can be used to set a site-wide default
  • Supported Parameters:
    Parameter: Description: Default:
    "on" / "off" Switch to control TT usage for the current topic. A value "on" or "1" will activate TT processing for the current topic, every other value will disable it. To retain the configuration setting, don't specify this parameter. Config variable {UseTT}
    WRAPPER="tt_template" The TT wrapper template into which the topic will be embedded. The wrapper must explicitly include the topic content, which is available as a TT variable [% content %]. no wrapper

Configuration Variables

Not fully documented, until I figure out how http://stlchina.huhoo.net/twiki/bin/configure.pl will pick up plugin configuration vars.

All configuration variables for this plugin are collected under $TWiki::cfg{Plugins}{TemplateToolkitPlugin}.

Configuration Variable Description
{UseTT} Sitewide default for TT usage. Can be overridden in a topic with the %TEMPLATETOOLKIT{...}% tag.
{TToptions} Hash reference passed as \%config parameter to Template->new(). Refer to the TT documentation for details.
Per default, the current topic's attachment directory is used as INCLUDE_PATH, and the START_TAG option is basically the familiar [%, but adjusted to take care for TWiki forced link/variable combinations.

Notes on Template Toolkit Syntax

If you are familiar with the Template Toolkit, then you surely know that it per default embeds its directives between the delimiters [% and %]. As almost everything in TT this is configurable, but the default delimiters are used quite frequently because they usually don't occur in "normal text". Usually. In TWiki, there is a syntax construction which is quite popular, but makes TT croak: Forced links, with an embedded TWiki variable. Example:

This is a valid TWiki link pointing to TWiki.WebStatistics, but an invalid TT construct [% TWIKIWEB%.%STATISTICSTOPIC %] within an additional set of square brackets.

This plugin tries hard to cope with this, so the above example will work as intended. Some recommendations, just in case:

  • It is good practice to write your TT constructs as [% tt_directive %], i.e. with surrounding spaces around directives. By doing so, you'll make sure to never clash with a possible TWiki variable name %tt_directive%.
  • Separate TT stuff from heavy TWiki lifting or you'll get severe bracketiquotitis.
  • If you want to use different delimiters, define both {TToptions}{START_TAG} and {TToptions}{END_TAG} in $TWiki::cfg{Plugins}{TemplateToolkitPlugin}.
  • When writing TT wrappers for use with this plugin, keep in mind that they are TT-expanded, but not TML-processed.


Per default, TemplateToolkitPlugin ships with a simple TT demo wrapper. To see it in action, prepend a topic with the following line:

%TEMPLATETOOLKIT{"on" WRAPPER="demowrapper.tt2"}%

TT expansion can be controlled by URL parameters as well. If the plugin is installed, you can apply the demonstration wrapper to the current topic by clicking here.

Security Considerations

Unlike in typical TT web applications, in a TWiki many people can edit the templates, and so they can introduce TT directives. To keep your TWiki unharmed, some precautions should be made:

  1. Do not enable EVAL_PERL (it is off per default).
  2. Do not pass critical data structures like the configuration hash as TT variables. Remember that with TT directives it is possible to change data, and to call object methods. With a persistent interpreter like mod_perl, this could lead to a corrupt configuration for subsequent requests.

Future Directions

If time permits, I'll try to:

  • create a TWiki skin and a TT layout in parallel and run benchmarks on both.
  • document (and extend) configuration items
  • Add a TWiki filter for TT templates so that wrappers can use TWiki variables
  • ...

Plugin Installation Instructions

You do not need to install anything in the browser to use this extension. The following instructions are for the administrator who installs the extension on the server where TWiki is running.

Like many other TWiki extensions, this module is shipped with a fully automatic installer script written using the BuildContrib.

  • If you have TWiki 4.2 or later, you can install from the configure interface (Go to Plugins->Find More Extensions)
  • If you have any problems, then you can still install manually from the command-line:
    1. Download one of the .zip or .tgz archives
    2. Unpack the archive in the root directory of your TWiki installation.
    3. Run the installer script ( perl <module>_installer )
    4. Run configure and enable the module, if it is a plugin.
    5. Repeat for any missing dependencies.
  • If you are still having problems, then instead of running the installer script:
    1. Make sure that the file permissions allow the webserver user to access all files.
    2. Check in any installed files that have existing ,v files in your existing install (take care not to lock the files when you check in)
    3. Manually edit LocalSite.cfg to set any configuration variables.

Plugin Info

Author: TWiki:Main/HaraldJoerg
Copyright ©: 2006, TWiki:Main/HaraldJoerg, All Rights Reserved
License: GPL (GNU General Public License)
TWiki Dependency: $TWiki::Plugins::VERSION 1.1
Template>=2.000Required. Available from CPAN.
Other Dependencies: none
Perl Version: 5.8
Version: 15567 (12 Dec 2008)
Change History:  
21 Sep 2006: Initial version
TWiki:Plugins/Benchmark: unknown
Home: http://TWiki.org/cgi-bin/view/Plugins/TemplateToolkitPlugin
Feedback: http://TWiki.org/cgi-bin/view/Plugins/TemplateToolkitPluginDev
Appraisal: http://TWiki.org/cgi-bin/view/Plugins/TemplateToolkitPluginAppraisal

Related Topics: TWikiPlugins, DeveloperDocumentationCategory, AdminDocumentationCategory, TWikiPreferences

-- TWiki:Main.HaraldJoerg - 21 Sep 2006

Topic attachments
I Attachment Action Size Date Who Comment
Unknown file formattt2 demowrapper.tt2 manage 0.1 K 1970-01-01 - 00:00 UnknownUser A simple grey box around the topic text
Topic revision: r1 - 2005-03-27 - TWikiContributor
This site is powered by the TWiki collaboration platformCopyright © 1999-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback 京ICP备05049167号
Note: Please contribute updates to this topic on TWiki.org at TWiki:TWiki.TemplateToolkitPlugin.