Template::Alloy::TT(3pm) | User Contributed Perl Documentation | Template::Alloy::TT(3pm) |
Template::Alloy::TT - Template::Toolkit role
The Template::Alloy::TT role provides the syntax and the interface for Template::Toolkit version 1, 2, and 3. It also brings many of the features from the various templating systems.
And it is fast.
See the Template::Alloy documentation for configuration and other parameters.
Alloy uses the same base template syntax and configuration items as TT2, but the internals of Alloy were written from scratch. Additionally much of the planned TT3 syntax is supported as well as most of that of HTML::Template::Expr. The following is a list of some of the ways that the configuration and syntax of Alloy are different from that of TT2. Note: items that are planned to work in TT3 are marked with (TT3).
[% a = {1 => 2} %]
[% a = {"$foo" => 1} %]
[% a = [1..10, 21..30] %]
[% a = [1..10].reverse %] [% "$foo".length %] [% 123.length %] # = 3 [% 123.4.length %] # = 5 [% -123.4.length %] # = -5 ("." binds more tightly than "-") [% (a ~ b).length %] [% "hi".repeat(3) %] # = hihihi [% {a => b}.size %] # = 1
[% [0..10].${ 1 + 2 } %] # = 4 [% {ab => 'AB'}.${ 'a' ~ 'b' } %] # = AB [% color = qw/Red Blue/; FOR [1..4] ; color.${ loop.index % color.size } ; END %] # = RedBlueRedBlue
[% "foo".match( /(F\w+)/i ).0 %] # = foo
[% f = "[% (1 + 2) %]" %][% f|eval %] # = 3
[% [0..10].${ 2.3 } %] # = 3
[% GET GET %] # gets the variable named "GET" [% GET $GET %] # gets the variable who's name is stored in "GET"
[% a | length %] [% b . lower %]
[% a = {size => "foo"} %][% a.size %] # = foo [% a = {size => "foo"} %][% a|size %] # = 1 (size of hash)
[% "aa" | repeat(2) . length %] # = 4
Restores the behavior of the pipe operator to be compatible with TT2.
With V2PIPE = 1
[% PROCESS a | repeat(2) %] # = value of block or file a repeated twice
With V2PIPE = 0 (default)
[% PROCESS a | repeat(2) %] # = process block or file named a ~ a
Allows for turning off TT2 "==" behavior. Defaults to 1 in TT syntaxes and to 0 in HT syntaxes.
[% CONFIG V2EQUALS => 1 %][% ('7' == '7.0') || 0 %] [% CONFIG V2EQUALS => 0 %][% ('7' == '7.0') || 0 %]
Prints
0 1
Default false. If true, will automatically call eval filter on double quoted strings.
Default false. If true, will leave in place interpolated values that weren't defined. You can then use the Velocity notation $!foo to not show these values.
The Text, List, and Hash types give direct access to virtual methods.
[% a = "foobar" %][% Text.length(a) %] # = 6 [% a = [1 .. 10] %][% List.size(a) %] # = 10 [% a = {a=>"A", b=>"B"} ; Hash.size(a) %] = 2 [% foo = {a => 1, b => 2} | Hash.keys | List.join(", ") %] # = a, b
[% list.fmt("%s", ", ") %] [% hash.fmt("%s => %s", "\n") %]
The following vmethods were added - they correspond to the perl functions of the same name.
abs atan2 cos exp hex lc log oct sin sprintf sqrt srand uc
[% sprintf("%d %d", 7, 8) %] # = "7 8"
The following are equivalent in Alloy:
[% "abc".length %] [% length("abc") %]
This feature may be disabling by setting the VMETHOD_FUNCTIONS configuration item to 0.
This is similar to how HTML::Template::Expr operates, but now you can use this functionality in TT templates as well.
[% 2-1 %] # = 1 (fails in TT2)
[% 2 ** 3 %] [% 2 pow 3 %] # = 8 8
[% IF "a" lt "b" %]a is less[% END %]
This can be used to make up for the fact that TT2 made == the same as eq (which will hopefully change - use eq when you mean eq).
[% IF ! (a <=> b) %]a == b[% END %] [% IF (a <=> b) %]a != b[% END %]
[% a = 2; a *= 3 ; a %] # = 6 [% a = 2; (a *= 3) ; a %] # = 66
[% ++a ; ++a %] # = 12 [% a-- ; a-- %] # = 0-1
[% a = qw(a b c); a.1 %] # = b [% qw/a b c/.2 %] # = c
[% "FOO".match(/(foo)/i).0 %] # = FOO [% a = /(foo)/i; "FOO".match(a).0 %] # = FOO
[% a = 1.2e-20 %] [% 123.fmt('%.3e') %] # = 1.230e+02
[% a = 0xff0000 %][% a %] # = 16711680 [% a = 0xff2 / 0xd; a.fmt('%x') %] # = 13a
[% FOREACH f.b = [1..10] ; f.b ; END %]
Note that nested variables are subject to scoping issues. f.b will not be reset to its value before the FOREACH.
Andy Wardley calls this side-by-side effect notation.
[% one IF two IF three %] same as [% IF three %][% IF two %][% one %][% END %][% END %] [% a = [[1..3], [5..7]] %][% i FOREACH i = j FOREACH j = a %] # = 123567
[% SET a = 1 GET a %] [% FOREACH i = [1 .. 10] i END %]
Note: a semi-colon is still required in front of any block directive that can be used as a post-operative directive.
[% 1 IF 0 2 %] # prints 2 [% 1; IF 0 2 END %] # prints 1
Note2: This behavior can be disabled by setting the SEMICOLONS configuration item to a true value. If SEMICOLONS is true, then a SEMICOLON must be set after any directive that isn't followed by a post-operative directive.
TT2 requires them to contain something.
Used for Data::Dumper'ing the passed variable or expression.
[% DUMP a.a %]
[% CONFIG ANYCASE => 1 PRE_CHOMP => '-' %]
my $t = Template::Alloy->new({ anycase => 1, interpolate => 1, });
[%- var = [{key => 'a'}, {key => 'b'}] %] [%- LOOP var %] ([% key %]) [%- END %] Prints (a) (b)
[% CONFIG SYNTAX => 'hte' %] [% var = '<TMPL_VAR EXPR="sprintf('%s', 'hello world')">' %] [% var | eval %]
Alloy has introduced the CALL_CONTEXT configuration item which defaults to "smart," but can also be set to "list" or "item." List context will always return an arrayref from called functions and methods and will call in list context. Item context will always call in item (scalar) context and will return one item.
The @() and $() operators allow for functions embedded inside to use list and item context (respectively). They are modeled after the corresponding Perl 6 context specifiers. See the Template::Alloy::Operators perldoc and CALL_CONTEXT configuration documentation for more information.
[% array = @( this.get_rows ) %] [% item = $( this.get_something ) %]
The ->() operator behaves similarly to the MACRO directive, but can be used to pass functions to map, grep, and sort vmethods.
[% MACRO foo(n) BLOCK %]Say [% n %][% END %] [% foo = ->(n){ "Say $n" } %] [% [0..10].grep(->(this % 2)).join %] prints 3 5 7 9 [% ['a' .. 'c'].map(->(a){ a.upper }).join %] prints A B C [% [1,2,3].sort(->(a,b){ b <=> a }).join %] prints 3 2 1
[% a = ->(n){ [1..n].return } %]
It generates an "opcode" tree. The opcode tree is an arrayref of scalars and array refs nested as deeply as possible. This "simple" structure could be shared TT implementations in other languages via JSON or YAML. You can optionally enable generating Perl code by setting COMPILE_PERL = 1.
If EVAL_PERL is off, Alloy will not eval_string on ANY piece of information.
You can control the nested nature of eval_filter and MACRO recursion using the MAX_EVAL_RECURSE and MAX_MACRO_RECURSE configuration items.
Alloy provides a context object that mimics the Template::Context interface for use by some TT filters, eval perl blocks, views, and plugins.
Alloy uses the load_template method to get and cache templates.
Alloy has its own built-in recursive regex based parser and grammar system.
Alloy can actually be substituted in place of the native Template::Parser and Template::Grammar in TT by using the Template::Parser::Alloy module. This module uses the output of parse_tree to generate a TT style compiled perl document.
It only understands DEBUG_DIRS (8) and DEBUG_UNDEF (2).
When debug dirs is on, directives on different lines separated by colons show the line they are on rather than a general line range.
Parse errors actually know what line and character they occurred at.
Full support is offered for the PLUGINS and LOAD_PERL configuration items.
Also note that Template::Alloy only has native support for the Iterator plugin. Any of the other plugins requested will need to provided by installing Template::Toolkit or the appropriate plugin module.
Full support is offered for the FILTERS configuration item.
Paul Seamons <paul@seamons.com>
This module may be distributed under the same terms as Perl itself.
2022-10-16 | perl v5.36.0 |