Template::Manual::Plugins - Standard plugins


NAME

Template::Manual::Plugins - Standard plugins


DESCRIPTION

This section lists the standard plugins which can be used to extend the runtime functionality of the Template Toolkit. The plugins are distributed with the Template Toolkit but may required additional modules from CPAN.


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 the Template::Plugin::Autoformat manpage and the Text::Autoformat manpage 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 the Template::Plugin::CGI manpage) 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 the Template::Plugin::Datafile manpage 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 the Template::Plugin::Date manpage and the POSIX manpage 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 the Template::Plugin::Directory manpage 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, 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 the Template::Plugin::DBI manpage and the DBI manpage 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 the Template::Plugin::Dumper manpage and the Data::Dumper manpage for futher 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 the Template::Plugin::File manpage 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;
    }
    # now load it...
    [% USE MyFilter %]
    # ...and use the returned object as a filter
    [% FILTER $MyFilter %]
      ...
    [% END %]

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

Format

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

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

GD::Image, GD::Polygon, GD::Constants

These plugins provide access to the GD graphics library via Lincoln D. Stein's GD.pm interface. These plugins allow PNG, JPEG and other graphical formats to be generated.

    [% FILTER null;
        USE im = GD.Image(100,100);
        # allocate some colors
        black = im.colorAllocate(0,   0, 0);
        red   = im.colorAllocate(255,0,  0);
        blue  = im.colorAllocate(0,  0,  255);
        # Draw a blue oval
        im.arc(50,50,95,75,0,360,blue);
        # And fill it with red
        im.fill(50,50,red);
        # Output image in PNG format
        im.png | stdout(1);
       END;
    -%]

See the Template::Plugin::GD::Image manpage for further details.

GD::Text, GD::Text::Align, GD::Text::Wrap

These plugins provide access to Martien Verbruggen's GD::Text, GD::Text::Align and GD::Text::Wrap modules. These plugins allow the layout, alignment and wrapping of text when drawing text in GD images.

    [% FILTER null;
        USE gd  = GD.Image(200,400);
        USE gdc = GD.Constants;
        black = gd.colorAllocate(0,   0, 0);
        green = gd.colorAllocate(0, 255, 0);
        txt = "This is some long text. " | repeat(10);
        USE wrapbox = GD.Text.Wrap(gd,
         line_space  => 4,
         color       => green,
         text        => txt,
        );
        wrapbox.set_font(gdc.gdMediumBoldFont);
        wrapbox.set(align => 'center', width => 160);
        wrapbox.draw(20, 20);
        gd.png | stdout(1);
      END;
    -%]

See the Template::Plugin::GD::Text manpage, the Template::Plugin::GD::Text::Align manpage and the Template::Plugin::GD::Text::Wrap manpage for further details.

GD::Graph::lines, GD::Graph::bars, GD::Graph::points, GD::Graph::linespoin ts, GD::Graph::area, GD::Graph::mixed, GD::Graph::pie

These plugins provide access to Martien Verbruggen's GD::Graph module that allows graphs, plots and charts to be created. These plugins allow graphs, plots and charts to be generated in PNG, JPEG and other graphical formats.

    [% FILTER null;
        data = [
            ["1st","2nd","3rd","4th","5th","6th"],
            [    4,    2,    3,    4,    3,  3.5]
        ];
        USE my_graph = GD.Graph.pie(250, 200);
        my_graph.set(
                title => 'A Pie Chart',
                label => 'Label',
                axislabelclr => 'black',
                pie_height => 36,
                transparent => 0,
        );
        my_graph.plot(data).png | stdout(1);
      END;
    -%]

See the Template::Plugin::GD::Graph::lines manpage, the Template::Plugin::GD::Graph::bars manpage, the Template::Plugin::GD::Graph::points manpage, the Template::Plugin::GD::Graph::linespoints manpage, the Template::Plugin::GD::Graph::area manpage, the Template::Plugin::GD::Graph::mixed manpage, the Template::Plugin::GD::Graph::pie manpage, and the GD::Graph manpage, for more details.

GD::Graph::bars3d, GD::Graph::lines3d, GD::Graph::pie3d

These plugins provide access to Jeremy Wadsack's GD::Graph3d module. This allows 3D bar charts and 3D lines plots to be generated.

    [% FILTER null;
        data = [
            ["1st","2nd","3rd","4th","5th","6th","7th", "8th", "9th"],
            [    1,    2,    5,    6,    3,  1.5,    1,     3,     4],
        ];
        USE my_graph = GD.Graph.bars3d();
        my_graph.set(
            x_label         => 'X Label',
            y_label         => 'Y label',
            title           => 'A 3d Bar Chart',
            y_max_value     => 8,
            y_tick_number   => 8,
            y_label_skip    => 2,
            # shadows
            bar_spacing     => 8,
            shadow_depth    => 4,
            shadowclr       => 'dred',
            transparent     => 0,
        my_graph.plot(data).png | stdout(1);
      END;
    -%]

See the Template::Plugin::GD::Graph::lines3d manpage, the Template::Plugin::GD::Graph::bars3d manpage, and the Template::Plugin::GD::Graph::pie3d manpage for more details.

HTML

The HTML plugin is very new and 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).

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

See the Template::Plugin::HTML manpage 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 the Template::Plugin::Iterator manpage for further details.

    [% USE iterator(list, args) %]
    [% FOREACH item = iterator %]
       [% '<ul>' IF iterator.first %]
       <li>[% item %]
       [% '</ul>' IF iterator.last %]
    [% END %]

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
    %]

String

The String plugin implements an object-oriented interface for manipulating strings. See the Template::Plugin::String manpage 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 the Template::Plugin::Table manpage 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 the Template::Plugin::URL manpage 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 the Template::Plugin::Wrap manpage and the Text::Wrap manpage 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 the Template::Plugin::XML::DOM manpage and the XML::DOM manpage 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::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 the Template::Plugin::XML::RSS manpage and the XML::RSS manpage 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/

XML::Simple

This plugin implements an interface to the XML::Simple module.

    [% USE xml = XML.Simple(xml_file_or_text) %]
    [% xml.head.title %]

See the Template::Plugin::XML::Simple manpage for further details.

XML::Style

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

    [% USE xmlstyle 
           table = { 
               attributes = { 
                   border      = 0
                   cellpadding = 4
                   cellspacing = 1
               }
           }
    %]
    [% FILTER xmlstyle %]
    <table>
    <tr>
      <td>Foo</td> <td>Bar</td> <td>Baz</td>
    </tr>
    </table>
    [% END %]

See the Template::Plugin::XML::Style manpage for further details.

XML::XPath

The XML::XPath plugin provides an interface to Matt Sergeant's <matt@sergeant.org> XML::XPath module. See the Template::Plugin::XML::XPath manpage and the XML::XPath manpage 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/


AUTHOR

Andy Wardley <abw@andywardley.com>

http://www.andywardley.com/|http://www.andywardley.com/


VERSION

Template Toolkit version 2.10, released on 24 July 2003.


COPYRIGHT

  Copyright (C) 1996-2003 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.

 Template::Manual::Plugins - Standard plugins