Andy Wardley > Template-Toolkit-2.00 > Template::Plugins

Download:
Template-Toolkit-2.00.tar.gz

Dependencies

Annotate this POD

CPAN RT

New  48
Open  17
View/Report Bugs
Module Version: 2.04   Source  

NAME ^

Template::Plugins - provider module for loading and instantiating plugins

SYNOPSIS ^

    use Template::Plugins;

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

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

DESCRIPTION ^

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

METHODS ^

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 ],
    });

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.

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 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 single value or as a reference to an array of multiple values. The default PLUGIN_BASE value, 'Template::Plugin', is always added the the end of the PLUGIN_BASE list (a single value is first converted to a list). Each value should contain a Perl package name to which the requested plugin name is appended.

example 1:

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

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

example 2:

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

    [% USE Foo %]    # =>   MyOrg::Template::Plugin::Foo
                       or YourOrg::Template::Plugin::Foo 
                       or          Template::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.

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.

Autoformat

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

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

The Text::Autoformat module is available from CPAN:

    http://www.cpan.org/modules/by-module/Text/
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 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 int he 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) %]
DBI

The DBI plugin, developed by Simon Matthews <sam@knowledgepool.com>, brings the full power of Tim Bunce's <Tim.Bunce@ig.co.uk> database interface module (DBI) to your templates. See Template::Plugin::DBI and DBI for further details.

    [% USE DBI('dbi:driver:database', 'user', 'pass') %]

    [% FOREACH user = DBI.query( 'SELECT * FROM users' ) %]
       [% user.id %] [% user.name %]
    [% END %]

The DBI and relevant DBD modules are available from CPAN:

  http://www.cpan.org/modules/by-module/DBI/
Dumper

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

    [% USE dumper(indent=0, pad="<br>") %]
    [% dumper.dump(myvar, yourvar) %]
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') %]
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 %]
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 contructing 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
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 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::DOM

The XML::DOM plugin gives access to the XML Document Object Module via Clark Cooper <cooper@sch.ge.com> and Enno Derksen's <enno@att.com> XML::DOM module. See Template::Plugin::XML::DOM and XML::DOM for further details.

    [% USE dom = XML.DOM %]
    [% doc = dom.parse(filename) %]

    [% FOREACH node = doc.getElementsByTagName('CODEBASE') %]
       * [% node.getAttribute('href') %]
    [% END %]

The plugin requires the XML::DOM module, available from CPAN:

    http://www.cpan.org/modules/by-module/XML/
XML::XPath

The XML::XPath plugin provides an interface to Matt Sergeant's <matt@sergeant.org> XML::XPath module. See Template::Plugin::XML::XPath and XML::XPath for further details.

    [% USE xpath = XML.XPath(xmlfile) %]
    [% FOREACH page = xpath.findnodes('/html/body/page') %]
       [% page.getAttribute('title') %]
    [% END %]

The plugin requires the XML::XPath module, available from CPAN:

    http://www.cpan.org/modules/by-module/XML/
XML::RSS

The XML::RSS plugin is a simple interface to Jonathan Eisenzopf's <eisen@pobox.com> XML::RSS module. A RSS (Rich Site Summary) file is typically used to store short news 'headlines' describing different links within a site. This plugin allows you to parse RSS files and format the contents accordingly using templates. See Template::Plugin::XML::RSS and XML::RSS for further details.

    [% USE news = XML.RSS(filename) %]
   
    [% FOREACH item = news.items %]
       <a href="[% item.link %]">[% item.title %]</a>
    [% END %]

The XML::RSS module is available from CPAN:

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

BUGS / ISSUES ^

AUTHOR ^

Andy Wardley <abw@kfs.org>

    http://www.template-toolkit.org/
    http://www.kfs.org/~abw/

REVISION ^

$Revision: 1.1 $

COPYRIGHT ^

    Copyright (C) 1996-2000 Andy Wardley.  All Rights Reserved.
    Copyright (C) 1998-2000 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.

SEE ALSO ^

Template, Template::Plugin, Template::Context

syntax highlighting: