[%# # IMPORTANT NOTE # This documentation is generated automatically from source # templates. Any changes you make here may be lost. # # The 'docsrc' documentation source bundle is available for download # from http://www.template-toolkit.org/docs.html and contains all # the source templates, XML files, scripts, etc., from which the # documentation for the Template Toolkit is built. -%] [% META book = 'Modules' page = 'Plugins' %] [% WRAPPER toc; PROCESS tocitem title ="SYNOPSIS" subs = []; PROCESS tocitem title ="DESCRIPTION" subs = []; PROCESS tocitem title ="METHODS" subs = [ "new(\\%params) ", "fetch(\$name, @args)" ]; PROCESS tocitem title ="CONFIGURATION OPTIONS" subs = []; PROCESS tocitem title ="TEMPLATE TOOLKIT PLUGINS" subs = [ "Autoformat", "CGI", "Datafile", "Date", "Directory", "DBI", "Dumper", "File", "Filter", "Format", "GD", "HTML", "Iterator", "Pod", "String", "Table", "URL", "Wrap", "XML::Style", "XML" ]; PROCESS tocitem title ="BUGS / ISSUES" subs = []; PROCESS tocitem title ="AUTHOR" subs = []; PROCESS tocitem title ="VERSION" subs = []; PROCESS tocitem title ="COPYRIGHT" subs = []; PROCESS tocitem title ="SEE ALSO" subs = []; END %] [% WRAPPER section title="SYNOPSIS" -%]

        use Template::Plugins;

        $plugin_provider = Template::Plugins->new(\%options);

        ($plugin, $error) = $plugin_provider->fetch($name, @args);

[%- END %] [% WRAPPER section title="DESCRIPTION" -%]

The Template::Plugins module defines a provider class which can be used to load and instantiate Template Toolkit plugin modules.

[%- END %] [% WRAPPER section title="METHODS" -%][% WRAPPER subsection title = "new(\\%params) " -%]

Constructor method which instantiates and returns a reference to a Template::Plugins object. A reference to a hash array of configuration items may be passed as a parameter. These are described below.

Note that the Template.pm front-end module creates a Template::Plugins provider, passing all configuration items. Thus, the examples shown below in the form:

        $plugprov = Template::Plugins->new({
    	PLUGIN_BASE => 'MyTemplate::Plugin',
            LOAD_PERL   => 1,
    	...
        });

can also be used via the Template module as:

        $ttengine = Template->new({
    	PLUGIN_BASE => 'MyTemplate::Plugin',
            LOAD_PERL   => 1,
    	...
        });

as well as the more explicit form of:

        $plugprov = Template::Plugins->new({
    	PLUGIN_BASE => 'MyTemplate::Plugin',
            LOAD_PERL   => 1,
    	...
        });

        $ttengine = Template->new({
    	LOAD_PLUGINS => [ $plugprov ],
        });

[%- END %] [% WRAPPER subsection title = "fetch(\$name, @args)" -%]

Called to request that a plugin of a given name be provided. The relevant module is first loaded (if necessary) and the load() class method called to return the factory class name (usually the same package name) or a factory object (a prototype). The new() method is then called as a class or object method against the factory, passing all remaining parameters.

Returns a reference to a new plugin object or ($error, STATUS_ERROR) on error. May also return (undef, STATUS_DECLINED) to decline to serve the request. If TOLERANT is set then all errors will be returned as declines.

[%- END %] [%- END %] [% WRAPPER section title="CONFIGURATION OPTIONS" -%]

The following list details the configuration options that can be provided to the Template::Plugins new() constructor.

- PLUGINS

  The PLUGINS options can be used to provide a reference to a hash array that maps plugin names to Perl module names. A number of standard plugins are defined (e.g. 'table', 'cgi', 'dbi', etc.) which map to their corresponding Template::Plugin::* counterparts. These can be redefined by values in the PLUGINS hash.

          my $plugins = Template::Plugins->new({
              PLUGINS => {
                  cgi => 'MyOrg::Template::Plugin::CGI',
                  foo => 'MyOrg::Template::Plugin::Foo',
                  bar => 'MyOrg::Template::Plugin::Bar',
              },
          });

  The recommended convention is to specify these plugin names in lower case. The Template Toolkit first looks for an exact case-sensitive match and then tries the lower case conversion of the name specified.

          [% tt_start_tag %] USE Foo [% tt_end_tag %]      # look for 'Foo' then 'foo'

  If you define all your PLUGINS with lower case names then they will be located regardless of how the user specifies the name in the USE directive. If, on the other hand, you define your PLUGINS with upper or mixed case names then the name specified in the USE directive must match the case exactly.

  The USE directive is used to create plugin objects and does so by calling the plugin() method on the current Template::Context object. If the plugin name is defined in the PLUGINS hash then the corresponding Perl module is loaded via require(). The context then calls the load() class method which should return the class name (default and general case) or a prototype object against which the new() method can be called to instantiate individual plugin objects.

  If the plugin name is not defined in the PLUGINS hash then the PLUGIN_BASE and/or LOAD_PERL options come into effect.

- PLUGIN_BASE

  If a plugin is not defined in the PLUGINS hash then the PLUGIN_BASE is used to attempt to construct a correct Perl module name which can be successfully loaded.

  The PLUGIN_BASE can be specified as a reference to an array of module namespaces, or as a single value which is automatically converted to a list. The default PLUGIN_BASE value ('Template::Plugin') is then added to the end of this list.

  example 1:

          my $plugins = Template::Plugins->new({
              PLUGIN_BASE => 'MyOrg::Template::Plugin',
          });

          [% tt_start_tag %] USE Foo [% tt_end_tag %]    # => MyOrg::Template::Plugin::Foo
                             or        Template::Plugin::Foo

  example 2:

          my $plugins = Template::Plugins->new({
              PLUGIN_BASE => [   'MyOrg::Template::Plugin',
                                 'YourOrg::Template::Plugin'  ],
          });

          [% tt_start_tag %] USE Foo [% tt_end_tag %]    # =>   MyOrg::Template::Plugin::Foo
                             or YourOrg::Template::Plugin::Foo
                             or          Template::Plugin::Foo

  If you don't want the default Template::Plugin namespace added to the end of the PLUGIN_BASE, then set the $Template::Plugins::PLUGIN_BASE variable to a false value before calling the Template::Plugins new() constructor method. This is shown in the example below where the 'Foo' is located as 'My::Plugin::Foo' or 'Your::Plugin::Foo' but not as 'Template::Plugin::Foo'.

  example 3:

          use Template::Plugins;
          $Template::Plugins::PLUGIN_BASE = '';

          my $plugins = Template::Plugins->new({
              PLUGIN_BASE => [   'My::Plugin',
                                 'Your::Plugin'  ],
          });

          [% tt_start_tag %] USE Foo [% tt_end_tag %]    # =>   My::Plugin::Foo
                             or Your::Plugin::Foo

- LOAD_PERL

  If a plugin cannot be loaded using the PLUGINS or PLUGIN_BASE approaches then the provider can make a final attempt to load the module without prepending any prefix to the module path. This allows regular Perl modules (i.e. those that don't reside in the Template::Plugin or some other such namespace) to be loaded and used as plugins.

  By default, the LOAD_PERL option is set to 0 and no attempt will be made to load any Perl modules that aren't named explicitly in the PLUGINS hash or reside in a package as named by one of the PLUGIN_BASE components.

  Plugins loaded using the PLUGINS or PLUGIN_BASE receive a reference to the current context object as the first argument to the new() constructor. Modules loaded using LOAD_PERL are assumed to not conform to the plugin interface. They must provide a new() class method for instantiating objects but it will not receive a reference to the context as the first argument. Plugin modules should provide a load() class method (or inherit the default one from the Template::Plugin base class) which is called the first time the plugin is loaded. Regular Perl modules need not. In all other respects, regular Perl objects and Template Toolkit plugins are identical.

  If a particular Perl module does not conform to the common, but not unilateral, new() constructor convention then a simple plugin wrapper can be written to interface to it.

- TOLERANT

  The TOLERANT flag is used by the various Template Toolkit provider modules (Template::Provider, Template::Plugins, Template::Filters) to control their behaviour when errors are encountered. By default, any errors are reported as such, with the request for the particular resource (template, plugin, filter) being denied and an exception raised. When the TOLERANT flag is set to any true values, errors will be silently ignored and the provider will instead return STATUS_DECLINED. This allows a subsequent provider to take responsibility for providing the resource, rather than failing the request outright. If all providers decline to service the request, either through tolerated failure or a genuine disinclination to comply, then a '<resource> not found' exception is raised.

- DEBUG

  The DEBUG option can be used to enable debugging messages from the Template::Plugins module by setting it to include the DEBUG_PLUGINS value.

          use Template::Constants qw( :debug );

          my $template = Template->new({
      	DEBUG => DEBUG_FILTERS | DEBUG_PLUGINS,
          });

[%- END %] [% WRAPPER section title="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.

[% WRAPPER subsection title = "Autoformat" -%]

The Autoformat plugin is an interface to Damian Conway's Text::Autoformat Perl module which provides advanced text wrapping and formatting. See [% ttlink('Template::Plugin::Autoformat') -%] and [% ttlink('Text::Autoformat') -%] for further details.

        [% tt_start_tag %] USE autoformat(left=10, right=20) [% tt_end_tag %]
        [% tt_start_tag %] autoformat(mytext) [% tt_end_tag %]	    # call autoformat sub
        [% tt_start_tag %] mytext FILTER autoformat [% tt_end_tag %]  # or use autoformat filter

The Text::Autoformat module is available from CPAN:

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

[%- END %] [% WRAPPER subsection title = "CGI" -%]

The CGI plugin is a wrapper around Lincoln Stein's <lstein@genome.wi.mit.edu> CGI.pm module. The plugin is distributed with the Template Toolkit (see [% ttlink('Template::Plugin::CGI') -%]) and the CGI module itself is distributed with recent versions Perl, or is available from CPAN.

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

[%- END %] [% WRAPPER subsection title = "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 [% ttlink('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:

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

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

[%- END %] [% WRAPPER subsection title = "Date" -%]

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

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

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

[%- END %] [% WRAPPER subsection title = "Directory" -%]

The Directory plugin provides a simple interface to a directory and the files within it. See [% ttlink('Template::Plugin::Directory') -%] for further details.

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

[%- END %] [% WRAPPER subsection title = "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-Plugin-DBI distribution from CPAN.

[%- END %] [% WRAPPER subsection title = "Dumper" -%]

The Dumper plugin provides an interface to the Data::Dumper module. See [% ttlink('Template::Plugin::Dumper') -%] and [% ttlink('Data::Dumper') -%] for futher details.

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

[%- END %] [% WRAPPER subsection title = "File" -%]

The File plugin provides a general abstraction for files and can be used to fetch information about specific files within a filesystem. See [% ttlink('Template::Plugin::File') -%] for further details.

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

[%- END %] [% WRAPPER subsection title = "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;
        }

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

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

See [% ttlink('Template::Plugin::Filter') -%] for further details.

[%- END %] [% WRAPPER subsection title = "Format" -%]

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

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

[%- END %] [% WRAPPER subsection title = "GD" -%]

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

[%- END %] [% WRAPPER subsection title = "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 (as discussed recently on the mod_perl mailing list).

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

See [% ttlink('Template::Plugin::HTML') -%] for further details.

[%- END %] [% WRAPPER subsection title = "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 [% ttlink('Template::Plugin::Iterator') -%] for further details.

        [% tt_start_tag %] USE iterator(list, args) [% tt_end_tag %]

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

[%- END %] [% WRAPPER subsection title = "Pod" -%]

This plugin provides an interface to the [% ttlink('Pod::POM', 'Pod::POM') -%] module which parses POD documents into an internal object model which can then be traversed and presented through the Template Toolkit.

        [% tt_start_tag %] USE Pod(podfile) [% tt_end_tag %]

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

[%- END %] [% WRAPPER subsection title = "String" -%]

The String plugin implements an object-oriented interface for manipulating strings. See [% ttlink('Template::Plugin::String') -%] for further details.

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

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

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

[%- END %] [% WRAPPER subsection title = "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 [% ttlink('Template::Plugin::Table') -%] for further details.

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

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

[%- END %] [% WRAPPER subsection title = "URL" -%]

The URL plugin provides a simple way of contructing URLs from a base part and a variable set of parameters. See [% ttlink('Template::Plugin::URL') -%] for further details.

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

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

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

[%- END %] [% WRAPPER subsection title = "Wrap" -%]

The Wrap plugin uses the Text::Wrap module by David Muir Sharnoff <muir@idiom.com> (with help from Tim Pierce and many many others) to provide simple paragraph formatting. See [% ttlink('Template::Plugin::Wrap') -%] and [% ttlink('Text::Wrap') -%] for further details.

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

The Text::Wrap module is available from CPAN:

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

[%- END %] [% WRAPPER subsection title = "XML::Style" -%]

This plugin defines a filter for performing simple stylesheet based transformations of XML text.

        [% tt_start_tag %] USE xmlstyle
               table = {
                   attributes = {
                       border      = 0
                       cellpadding = 4
                       cellspacing = 1
                   }
               }
        [% tt_end_tag %]

        [% tt_start_tag %] FILTER xmlstyle [% tt_end_tag %]
        <table>
        <tr>
          <td>Foo</td> <td>Bar</td> <td>Baz</td>
        </tr>
        </table>
        [% tt_start_tag %] END [% tt_end_tag %]

See [% ttlink('Template::Plugin::XML::Style') -%] for further details.

[%- END %] [% WRAPPER subsection title = "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.

[%- END %] [%- END %] [% WRAPPER section title="BUGS / ISSUES" -%]

- It might be worthwhile being able to distinguish between absolute module names and those which should be applied relative to PLUGIN_BASE directories. For example, use 'MyNamespace::MyModule' to denote absolute module names (e.g. LOAD_PERL), and 'MyNamespace.MyModule' to denote relative to PLUGIN_BASE.

[%- END %] [% WRAPPER section title="AUTHOR" -%]

Andy Wardley <abw@wardley.org>

[% ttlink('http://wardley.org/', 'http://wardley.org/') -%]

[%- END %] [% WRAPPER section title="VERSION" -%]

2.74, distributed as part of the Template Toolkit version 2.15, released on 26 May 2006.

[%- END %] [% WRAPPER section title="COPYRIGHT" -%]

      Copyright (C) 1996-2006 Andy Wardley.  All Rights Reserved.
      Copyright (C) 1998-2002 Canon Research Centre Europe Ltd.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

[%- END %] [% WRAPPER section title="SEE ALSO" -%]

[% ttlink('Template', 'Template') -%], [% ttlink('Template::Plugin', 'Template::Plugin') -%], [% ttlink('Template::Context', 'Template::Context') -%]

[%- END %]