NAME
Template::Tiny - Template Toolkit reimplemented in as little code as possible
SYNOPSIS
my $template = Template::Tiny->new(
TRIM => 1,
);
# Print the template results to STDOUT
$template->process( <<'END_TEMPLATE', { foo => 'World' } );
Hello [% foo %]!
END_TEMPLATE
DESCRIPTION
WARNING: THIS MODULE IS EXPERIMENTAL AND SUBJECT TO CHANGE WITHOUT NOTICE
YOU HAVE BEEN WARNED!
Template::Tiny is a reimplementation of a partial subset of the Template Toolkit in as few lines of code as possible.
It is intended for use in light-usage, low-memory, or low-cpu templating situations, where you may need to upgrade to the full feature set in the future, or if you want the familiarity of TT-style templates.
It is intended to have fully-compatible template and stash usage, with a limited by similar Perl API.
Unlike Template Toolkit, Template::Tiny will process templates without a compile phase (but despite this is still quicker, owing to heavy use of the Perl regular expression engine.
SUPPORTED USAGE
Only the default [% %]
tag style is supported.
Both the [%+ +%] style explicit whitespace and the [%- -%] style explicit chomp are support, although the [%+ +%] version is unneeded as Template::Tiny does not support default-enabled PRE_CHOMP or POST_CHOMP.
Variable expressions in the form foo.bar.baz are supported.
Appropriate simple behaviours for ARRAY reference, HASH reference and objects are supported, but not "VMethods" such as array lengths.
IF, ELSE and UNLESS conditions are supported, but only with simple foo.bar.baz conditions.
Support for looping is available, in the most simple [% FOREACH item IN list %] form.
All four IF/ELSE/UNLESS/FOREACH control structures are able to be nested to arbitrary depth.
The treatment of _private
hash and method keys is compatible with Template Toolkit, returning null or false rather than the actual content of the hash key or method.
Anything beyond this is currently out of scope
METHODS
new
my $template = Template::Tiny->new(
TRIM => 1,
);
The new
constructor is provided for compatibility with Template Toolkit.
The only parameter it currently supports is TRIM
(which removes leading and trailing whitespace from processed templates).
Additional parameters can be provided without error, but will be ignored.
process
# DEPRECATED: Return template results
my $text = $template->process( \$input, $vars );
# Print template results to STDOUT
$template->process( \$input, $vars );
# Generate template results into a variable
my $output = '';
$template->process( \$input, $vars, \$output );
The process
method is called to process a template.
The first parameter is a reference to a text string containing the template text. A reference to a hash may be passed as the second parameter containing definitions of template variables.
If a third parameter is provided, it must be a scalar reference to be populated with the output of the template.
For a limited amount of time, the old deprecated interface will continue to be supported. If process
is called without a third parameter, and in scalar or list contest, the template results will be returned to the caller.
If process
is called without a third parameter, and in void context, the template results will be print()ed to the currently selected file handle (probably STDOUT
) for compatibility with Template.
SUPPORT
Bugs should be reported via the CPAN bug tracker at
http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Template-Tiny
For other issues, or commercial enhancement or support, contact the author.
AUTHOR
Adam Kennedy <adamk@cpan.org>
SEE ALSO
COPYRIGHT
Copyright 2009 - 2010 Adam Kennedy.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module.