| HTML-Template-Filter-TT2 documentation | Contained in the HTML-Template-Filter-TT2 distribution. |
HTML::Template::Filter::TT2 - Template Toolkit 2 syntax for HTML::Template
Version 0.03
use HTML::Template::Filter::TT2;
my $tmpl = HTML::Template->new(filter => \&ht_tt2_filter, ...);
This HTML::Template filter allows you to use a subset of the Template Toolkit 2
syntax, which is much less verbose than the default syntax. This is not
an emulation of TT2, so you're still limited to the usual HTML::Template
semantics. Also, in order to keep the filter fast and simple, the
[% end %] must be written with the block name. See below for details.
Here is the syntax recognised by this module.
Simple interpolation:
[% variable %]
Interpolation with default value:
[% variable :default %]
Interpolation with filter (i.e. a HTML::Template escape mode):
[% variable |filter %]
Interpolation with default value and filter:
[% variable :default |filter %]
[% if condition %] ... [% else %] ... [end_if %]
The difference with the actual TT2 syntax is that you must use end_if
instead of end
[% loop loop-name %] ... [% end_loop %]
As for the if statement, you must use end_loop instead of end.
Exports the ht_tt2_filter function by default.
Pass a reference to this function to the filter parameter when calling
HTML::Template->new()
Sébastien Aperghis-Tramoni, <sebastien at aperghis.net>
HTML::Template, Template::Toolkit
Please report any bugs or feature requests to
bug-html-template-filter-tt2 at rt.cpan.org, or through the web interface at
http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTML-Template-Filter-TT2.
I will be notified, and then you'll automatically be notified of progress on
your bug as I make changes.
You can find documentation for this module with the perldoc command.
perldoc HTML::Template::Filter::TT2
You can also look for information at:
http://rt.cpan.org/NoAuth/Bugs.html?Dist=HTML-Template-Filter-TT2
Copyright 2007 Sébastien Aperghis-Tramoni, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| HTML-Template-Filter-TT2 documentation | Contained in the HTML-Template-Filter-TT2 distribution. |
package HTML::Template::Filter::TT2; use strict; require Exporter; { no strict "vars"; $VERSION = '0.03'; @ISA = qw(Exporter); @EXPORT = qw(ht_tt2_filter); }
sub ht_tt2_filter { my ($text_ref) = @_; $$text_ref =~ s{\[% *(loop|if|unless) +(\w+) *%\]}{<TMPL_\U$1\E $2>}gm; $$text_ref =~ s{\[% *(else) *%\]}{<TMPL_\U$1\E>}gm; $$text_ref =~ s{\[% *end_(\w+) *%\]}{</TMPL_\U$1\E>}gm; $$text_ref =~ s{ \[% # begin tag \s* (\w+) \s* # variable name \s* (?:: \s* (.+?))? # optional default value \s* (?:\| \s* (\w+))? # optional filter \s* %\] # end tag } {__format_variable($1, $2, $3)}gemx; } sub __format_variable { my ($var, $default, $filter) = @_; # variable name my $ht_syntax = "<TMPL_VAR NAME=$var"; # handle default value if (defined $default) { # autoquote unquoted values if ($default !~ /^["']/) { if ($default !~ /"/) { $default = qq/"$default"/ } elsif ($default !~ /'/) { $default = qq/'$default'/ } else { warn "Can't handle unquoted value '$default' for variable $var" } } $ht_syntax .= " DEFAULT=$default"; } # handle escape filter $ht_syntax .= " ESCAPE=$filter" if defined $filter; # end tag $ht_syntax .= ">"; return $ht_syntax; }
1; # End of HTML::Template::Filter::TT2