Template::Manual::Plugins(3)

NAME
Template::Manual::Plugins - Standard plugins

TEMPLATE TOOLKIT PLUGINS
The following plugin modules are distributed with the Template Toolkit. Some of the plugins interface to external modules (detailed below) which should be downloaded from any CPAN site and installed before using the plugin.

  Assert
New in 2.20! The Assert plugin adds an "assert" virtual method that you can use to catch undefined values.

For example, consider this dotop:

    [% user.name %]

If "user.name" is an undefined value then TT will silently ignore the fact and print nothing. If you "USE" the "assert" plugin then you can add the "assert" vmethod between the "user" and "name" elements, like so:

    [% user.assert.name %]

Now, if "user.name" is an undefined value, an exception will be thrown:

    assert error - undefined value for name

  CGI
The CGI plugin is a wrapper around Lincoln Stein's CGI.pm module. The plugin is distributed with the Template Toolkit (see Template::Plugin::CGI) and the CGI module itself is distributed with recent versions Perl, or is available from CPAN.

    [% USE CGI %]
    [% CGI.param('param_name') %]
    [% CGI.start_form %]
    [% CGI.popup_menu( Name   => 'color',
                       Values => [ 'Green', 'Brown' ] ) %]
    [% CGI.end_form %]

  Datafile
Provides an interface to data stored in a plain text file in a simple delimited format. The first line in the file specifies field names which should be delimiter by any non-word character sequence. Subsequent lines define data using the same delimiter as in the first line. Blank lines and comments (lines starting '#') are ignored. See Template::Plugin::Datafile for further details.

/tmp/mydata:

    # define names for each field
    id : email : name : tel
    # here's the data
    fred : fred@here.com : Fred Smith : 555-1234
    bill : bill@here.com : Bill White : 555-5678

example:

    [% USE userlist = datafile('/tmp/mydata') %]

    [% FOREACH user = userlist %]
       [% user.name %] ([% user.id %])
    [% END %]

  Date
The Date plugin provides an easy way to generate formatted time and date strings by delegating to the POSIX "strftime()" routine. See Template::Plugin::Date and POSIX for further details.

    [% USE date %]
    [% date.format %]           # current time/date

    File last modified: [% date.format(template.modtime) %]

  Directory
The Directory plugin provides a simple interface to a directory and the files within it. See Template::Plugin::Directory for further details.

    [% USE dir = Directory('/tmp') %]
    [% FOREACH file = dir.files %]
        # all the plain files in the directory
    [% END %]
    [% FOREACH file = dir.dirs %]
        # all the sub-directories
    [% END %]

  DBI
The "DBI" plugin is no longer distributed as part of the Template Toolkit (as of version 2.15). It is now available as a separate Template::DBI distribution from CPAN.

  Dumper
The Dumper plugin provides an interface to the Data::Dumper module. See Template::Plugin::Dumper and Data::Dumper for further details.

    [% USE dumper(indent=0, pad="<br>") %]
    [% dumper.dump(myvar, yourvar) %]

  File
The File plugin provides a general abstraction for files and can be used to fetch information about specific files within a filesystem. See Template::Plugin::File for further details.

    [% USE File('/tmp/foo.html') %]
    [% File.name %]     # foo.html
    [% File.dir %]      # /tmp
    [% File.mtime %]    # modification time

  Filter
This module implements a base class plugin which can be subclassed to easily create your own modules that define and install new filters.

    package MyOrg::Template::Plugin::MyFilter;

    use Template::Plugin::Filter;
    use base qw( Template::Plugin::Filter );

    sub filter {
        my ($self, $text) = @_;
        # ...mungify $text...
        return $text;
    }

Example of use:

    # now load it...
    [% USE MyFilter %]

    # ...and use the returned object as a filter
    [% FILTER $MyFilter %]
      ...
    [% END %]

See Template::Plugin::Filter for further details.

  Format
The Format plugin provides a simple way to format text according to a "printf()"-like format. See Template::Plugin::Format for further details.

    [% USE bold = format('<b>%s</b>') %]
    [% bold('Hello') %]

  GD
The "GD" plugins are no longer part of the core Template Toolkit distribution. They are now available from CPAN in a separate Template::GD distribution.

  HTML
The HTML plugin is very basic, implementing a few useful methods for generating HTML. It is likely to be extended in the future or integrated with a larger project to generate HTML elements in a generic way.

    [% USE HTML %]
    [% HTML.escape("if (a < b && c > d) ..." %]
    [% HTML.attributes(border => 1, cellpadding => 2) %]
    [% HTML.element(table => { border => 1, cellpadding => 2 }) %]

See Template::Plugin::HTML for further details.

  Iterator
The Iterator plugin provides a way to create a Template::Iterator object to iterate over a data set. An iterator is created automatically by the "FOREACH" directive and is aliased to the "loop" variable. This plugin allows an iterator to be explicitly created with a given name, or the default plugin name, "iterator". See Template::Plugin::Iterator for further details.

    [% USE iterator(list, args) %]

    [% FOREACH item = iterator %]
       [% '<ul>' IF iterator.first %]
       <li>[% item %]
       [% '</ul>' IF iterator.last %]
    [% END %]

  List
The List plugin provides an object-oriented interface to lists. See Template::Plugin::List for further details.

    [% USE list = List(1, 2, 3, 4, 5) %]

    [% list.size %]         # 5
    [% list.item(0) %]      # 1
    [% list.text %]         # 1, 2, 3, 4, 5

    [% CALL list.push(6) %]
    [% list.size %]         # 6

  Math
The Math plugin provides numerous mathematical functions for use within templates. It wraps Perl core math functions ("abs", "cos", "exp", "hex", "int", "log", "oct", "rand", "sin", "sqrt", etc.) and also makes additional trigonometric functions available if Math::Trig is installed. See Template::Plugin::Math for further details.

    [% USE Math %]

    [% Math.sqrt(9) %]      # 3
    [% Math.abs(-42) %]     # 42
    [% Math.hex('ff') %]    # 255

  Pod
This plugin provides an interface to the Pod::POM module which parses POD documents into an internal object model which can then be traversed and presented through the Template Toolkit.

    [% USE Pod(podfile) %]

    [% FOREACH head1 = Pod.head1;
         FOREACH head2 = head1/head2;
           ...
         END;
       END
    %]

  Scalar
The Template Toolkit calls user-defined subroutines and object methods using Perl's array context by default.

    # TT2 calls object methods in array context by default
    [% object.method %]

This plugin module provides a way for you to call subroutines and methods in scalar context.

    [% USE scalar %]

    # force it to use scalar context
    [% object.scalar.method %]

    # also works with subroutine references
    [% scalar.my_sub_ref %]

  String
The String plugin implements an object-oriented interface for manipulating strings. See Template::Plugin::String for further details.

    [% USE String 'Hello' %]
    [% String.append(' World') %]

    [% msg = String.new('Another string') %]
    [% msg.replace('string', 'text') %]

    The string "[% msg %]" is [% msg.length %] characters long.

  Table
The Table plugin allows you to format a list of data items into a virtual table by specifying a fixed number of rows or columns, with an optional overlap. See Template::Plugin::Table for further details.

    [% USE table(list, rows=10, overlap=1) %]

    [% FOREACH item = table.col(3) %]
       [% item %]
    [% END %]

  URL
The URL plugin provides a simple way of constructing URLs from a base part and a variable set of parameters. See Template::Plugin::URL for further details.

    [% USE mycgi = url('/cgi-bin/bar.pl', debug=1) %]

    [% mycgi %]
       # ==> /cgi/bin/bar.pl?debug=1

    [% mycgi(mode='submit') %]
       # ==> /cgi/bin/bar.pl?mode=submit&debug=1

  View
The View plugin creates Template::View objects. Views provide a mechanism for applying template-based "skins" to data objects, using a prefix and/or suffix to map method calls to templates. Views are an experimental feature. See Template::Plugin::View and Template::View for further details.

    [% USE view(prefix = 'my/') %]

    [% view.header(title = 'Hello') %]  # processes 'my/header'

  Wrap
The Wrap plugin uses the Text::Wrap module to provide simple paragraph formatting. See Template::Plugin::Wrap and Text::Wrap for further details.

    [% USE wrap %]
    [% wrap(mytext, 40, '* ', '  ') %]  # use wrap sub
    [% mytext FILTER wrap(40) -%]       # or wrap FILTER

The "Text::Wrap" module is available from CPAN:

    http://www.cpan.org/modules/by-module/Text/

  XML
The "XML::DOM", "XML::RSS", "XML::Simple" and "XML::XPath" plugins are no longer distributed with the Template Toolkit as of version 2.15

They are now available in a separate Template::XML distribution.