NAME

GT::Template - Gossamer Threads template parser


SYNOPSIS

    use GT::Template;
    my $var = GT::Template->parse('file.txt', { key => 'value' });
    ...
    print $var;

or

    use GT::Template;
    GT::Template->parse_print('file.txt', { key => 'value' });

or

    use GT::Template;
    GT::Template->parse_stream('file.txt', { key => 'value' });


DESCRIPTION

GT::Template provides a simple way (one line) to parse a template (which can be either a file or a string) and make sophisticated replacements.

It supports simple replacements, conditionals, function calls, including other templates, and more.

Additionally, through using pre-compiled files, subsequent parses of a template will be very fast.

Template Syntax

The template syntax documentation has moved - it is now documented in the GT::Template::Tutorial manpage.

parse

This option parses a template, and returns the value of the parsed template. See Parse Options for a description of the possible parse parameters.

parse_print

This option parses a template, and prints it. See Parse Options for a description of the possible parse_print parameters.

parse_stream

This option parses a template, and prints each part of it as the parse occurs. It should only be used in situations where streaming content is required as it is measurably slower than the parse_print alternative. See Parse Options for a description of the possible parse_stream parameters.

Parse Options

Filename

The first argument to parse()/parse_print()/parse_stream() (hereafter referred to simply as parse()) is the full or relative (to the current working directory) path to the file to parse.

Variables

The second argument is a hash reference of template variables that will be available in the parsed template (see the GT::Template::Tutorial manpage). Arbitrary hash/array data structure access is supported (see Advanced variables using references in the GT::Template::Tutorial manpage).

Loops are supported by providing an array reference or code reference as a value; array reference loops are generally preferred as they enable the loop to be used multiple times and support the <%loopvar.length%> syntax.

Options

The third argument (which is not required) takes additional options that change the way a parse is performed. The available options (there are more, however their use is discouraged) are as follows.

Aliases

The forth option to parse is an optional hash of aliases to set up for functions. The key should be the alias name and the value should be the function to call when the alias is invoked. For example:

    print GT::Template->parse(
        'file.htm',
        { key => 'value' },
        { compress => 1 },
        { myfunc => 'Long::Package::Name::To::myfunc' }
    );

Now in your template you can do:

    <%myfunc('argument')%>

Which will call Long::Package::Name::To::myfunc.

vars

Accessing variables from outside a template can be done by calling the GT::Template->vars method. For further details, please see the GT::Template::Vars manpage.


EXAMPLES

Parse the string contained in $template, making the 'key' tag available.

    my $parsed = GT::Template->parse(undef, { key => 'value' }, { string => $template });

Parse file.txt, compress the result, and print it. This is equivelant to print GT::Template->parse(...), but slightly faster.

    GT::Template->parse_print('file.txt', { key => 'value' }, { compress => 1 });

Print the output of the template it as it is parsed, not after entirely parsed. This will output the same as the above command would without the ``compress'' option, but is slower (unless, of course, streaming is needed).

    GT::Template->parse_stream('file.txt', { key => 'value' });

Don't display warnings on invalid keys:

    GT::Template->parse_print('file.txt', { key => 'value' }, { strict => 0 });


SEE ALSO

the GT::Template::Tutorial manpage - Documentation/tutorial for GT::Template template tags.

the GT::Template::Vars manpage - Interface for accessing/manipulating template tags from Perl code.

the GT::Template::Inheritance manpage - Documentation for GT::Template template inheritance.


COPYRIGHT

Copyright (c) 2005 Gossamer Threads Inc. All Rights Reserved. http://www.gossamer-threads.com/


VERSION

Revision: $Id: Template.pm,v 2.142 2005/07/05 00:39:40 jagerman Exp $