1Template::Alloy::TT(3)User Contributed Perl DocumentationTemplate::Alloy::TT(3)
2
3
4

NAME

6       Template::Alloy::TT - Template::Toolkit role
7

DESCRIPTION

9       The Template::Alloy::TT role provides the syntax and the interface for
10       Template::Toolkit version 1, 2, and 3.  It also brings many of the
11       features from the various templating systems.
12
13       And it is fast.
14
15       See the Template::Alloy documentation for configuration and other
16       parameters.
17

HOW IS Template::Alloy DIFFERENT FROM Template::Toolkit

19       Alloy uses the same base template syntax and configuration items as
20       TT2, but the internals of Alloy were written from scratch.
21       Additionally much of the planned TT3 syntax is supported as well as
22       most of that of HTML::Template::Expr.  The following is a list of some
23       of the ways that the configuration and syntax of Alloy are different
24       from that of TT2.  Note: items that are planned to work in TT3 are
25       marked with (TT3).
26
27       ·   Numerical hash keys work
28
29               [% a = {1 => 2} %]
30
31       ·   Quoted hash key interpolation is fine
32
33               [% a = {"$foo" => 1} %]
34
35       ·   Multiple ranges in same constructor
36
37               [% a = [1..10, 21..30] %]
38
39       ·   Constructor types can call virtual methods. (TT3)
40
41               [% a = [1..10].reverse %]
42
43               [% "$foo".length %]
44
45               [% 123.length %]   # = 3
46
47               [% 123.4.length %]  # = 5
48
49               [% -123.4.length %] # = -5 ("." binds more tightly than "-")
50
51               [% (a ~ b).length %]
52
53               [% "hi".repeat(3) %] # = hihihi
54
55               [% {a => b}.size %] # = 1
56
57       ·   The "${" and "}" variable interpolators can contain expressions,
58           not just variables.
59
60               [% [0..10].${ 1 + 2 } %] # = 4
61
62               [% {ab => 'AB'}.${ 'a' ~ 'b' } %] # = AB
63
64               [% color = qw/Red Blue/; FOR [1..4] ; color.${ loop.index % color.size } ; END %]
65                 # = RedBlueRedBlue
66
67       ·   You can use regular expression quoting.
68
69               [% "foo".match( /(F\w+)/i ).0 %] # = foo
70
71       ·   Tags can be nested.
72
73               [% f = "[% (1 + 2) %]" %][% f|eval %] # = 3
74
75       ·   Arrays can be accessed with non-integer numbers.
76
77               [% [0..10].${ 2.3 } %] # = 3
78
79       ·   Reserved names are less reserved. (TT3)
80
81               [% GET GET %] # gets the variable named "GET"
82
83               [% GET $GET %] # gets the variable who's name is stored in "GET"
84
85       ·   Filters and SCALAR_OPS are interchangeable. (TT3)
86
87               [% a | length %]
88
89               [% b . lower %]
90
91       ·   Pipe "|" can be used anywhere dot "." can be and means to call the
92           virtual method. (TT3)
93
94               [% a = {size => "foo"} %][% a.size %] # = foo
95
96               [% a = {size => "foo"} %][% a|size %] # = 1 (size of hash)
97
98       ·   Pipe "|" and "." can be mixed. (TT3)
99
100               [% "aa" | repeat(2) . length %] # = 4
101
102       ·   Added V2PIPE configuration item
103
104           Restores the behavior of the pipe operator to be compatible with
105           TT2.
106
107           With V2PIPE = 1
108
109               [% PROCESS a | repeat(2) %] # = value of block or file a repeated twice
110
111           With V2PIPE = 0 (default)
112
113               [% PROCESS a | repeat(2) %] # = process block or file named a ~ a
114
115       ·   Added V2EQUALS configuration item
116
117           Allows for turning off TT2 "==" behavior.  Defaults to 1 in TT
118           syntaxes and to 0 in HT syntaxes.
119
120               [% CONFIG V2EQUALS => 1 %][% ('7' == '7.0') || 0 %]
121               [% CONFIG V2EQUALS => 0 %][% ('7' == '7.0') || 0 %]
122
123           Prints
124
125               0
126               1
127
128       ·   Added AUTO_EVAL configuration item.
129
130           Default false.  If true, will automatically call eval filter on
131           double quoted strings.
132
133       ·   Added SHOW_UNDEFINED_INTERP configuration item.
134
135           Default false.  If true, will leave in place interpolated values
136           that weren't defined.  You can then use the Velocity notation $!foo
137           to not show these values.
138
139       ·   Added Virtual Object Namespaces. (TT3)
140
141           The Text, List, and Hash types give direct access to virtual
142           methods.
143
144               [% a = "foobar" %][% Text.length(a) %] # = 6
145
146               [% a = [1 .. 10] %][% List.size(a) %] # = 10
147
148               [% a = {a=>"A", b=>"B"} ; Hash.size(a) %] = 2
149
150               [% foo = {a => 1, b => 2}
151                  | Hash.keys
152                  | List.join(", ") %] # = a, b
153
154       ·   Added "fmt" scalar, list, and hash virtual methods.
155
156               [% list.fmt("%s", ", ") %]
157
158               [% hash.fmt("%s => %s", "\n") %]
159
160       ·   Added missing HTML::Template::Expr vmethods
161
162           The following vmethods were added - they correspond to the perl
163           functions of the same name.
164
165               abs
166               atan2
167               cos
168               exp
169               hex
170               lc
171               log
172               oct
173               sin
174               sprintf
175               sqrt
176               srand
177               uc
178
179       ·   Allow all Scalar vmethods to behave as top level functions.
180
181               [% sprintf("%d %d", 7, 8) %] # = "7 8"
182
183           The following are equivalent in Alloy:
184
185               [% "abc".length %]
186               [% length("abc") %]
187
188           This feature may be disabling by setting the VMETHOD_FUNCTIONS
189           configuration item to 0.
190
191           This is similar to how HTML::Template::Expr operates, but now you
192           can use this functionality in TT templates as well.
193
194       ·   Whitespace is less meaningful. (TT3)
195
196               [% 2-1 %] # = 1 (fails in TT2)
197
198       ·   Added pow operator.
199
200               [% 2 ** 3 %] [% 2 pow 3 %] # = 8 8
201
202       ·   Added string comparison operators (gt ge lt le cmp)
203
204               [% IF "a" lt "b" %]a is less[% END %]
205
206       ·   Added numeric comparison operator (<=>)
207
208           This can be used to make up for the fact that TT2 made == the same
209           as eq (which will hopefully change - use eq when you mean eq).
210
211               [% IF ! (a <=> b) %]a == b[% END %]
212
213               [% IF (a <=> b) %]a != b[% END %]
214
215       ·   Added self modifiers (+=, -=, *=, /=, %=, **=, ~=). (TT3)
216
217               [% a = 2;  a *= 3  ; a %] # = 6
218               [% a = 2; (a *= 3) ; a %] # = 66
219
220       ·   Added pre and post increment and decrement (++ --). (TT3)
221
222               [% ++a ; ++a %] # = 12
223               [% a-- ; a-- %] # = 0-1
224
225       ·   Added qw// contructor. (TT3)
226
227               [% a = qw(a b c); a.1 %] # = b
228
229               [% qw/a b c/.2 %] # = c
230
231       ·   Added regex contructor. (TT3)
232
233               [% "FOO".match(/(foo)/i).0 %] # = FOO
234
235               [% a = /(foo)/i; "FOO".match(a).0 %] # = FOO
236
237       ·   Allow for scientific notation. (TT3)
238
239               [% a = 1.2e-20 %]
240
241               [% 123.fmt('%.3e') %] # = 1.230e+02
242
243       ·   Allow for hexadecimal input. (TT3)
244
245               [% a = 0xff0000 %][% a %] # = 16711680
246
247               [% a = 0xff2 / 0xd; a.fmt('%x') %] # = 13a
248
249       ·   FOREACH variables can be nested.
250
251               [% FOREACH f.b = [1..10] ; f.b ; END %]
252
253           Note that nested variables are subject to scoping issues.  f.b will
254           not be reset to its value before the FOREACH.
255
256       ·   Post operative directives can be nested. (TT3)
257
258           Andy Wardley calls this side-by-side effect notation.
259
260               [% one IF two IF three %]
261
262               same as
263
264               [% IF three %][% IF two %][% one %][% END %][% END %]
265
266
267               [% a = [[1..3], [5..7]] %][% i FOREACH i = j FOREACH j = a %] # = 123567
268
269       ·   Semi-colons on directives in the same tag are optional. (TT3)
270
271               [% SET a = 1
272                  GET a
273                %]
274
275               [% FOREACH i = [1 .. 10]
276                    i
277                  END %]
278
279           Note: a semi-colon is still required in front of any block
280           directive that can be used as a post-operative directive.
281
282               [% 1 IF 0
283                  2 %]   # prints 2
284
285               [% 1; IF 0
286                  2
287                  END %] # prints 1
288
289           Note2: This behavior can be disabled by setting the SEMICOLONS
290           configuration item to a true value.  If SEMICOLONS is true, then a
291           SEMICOLON must be set after any directive that isn't followed by a
292           post-operative directive.
293
294       ·   CATCH blocks can be empty.
295
296           TT2 requires them to contain something.
297
298       ·   Added a DUMP directive.
299
300           Used for Data::Dumper'ing the passed variable or expression.
301
302              [% DUMP a.a %]
303
304       ·   Added CONFIG directive.
305
306              [% CONFIG
307                   ANYCASE   => 1
308                   PRE_CHOMP => '-'
309              %]
310
311       ·   Configuration options can use lowercase names instead of the all
312           uppercase names that TT2 uses.
313
314               my $t = Template::Alloy->new({
315                   anycase     => 1,
316                   interpolate => 1,
317               });
318
319       ·   Added LOOP directive (works the same as LOOP in HTML::Template.
320
321              [%- var = [{key => 'a'}, {key => 'b'}] %]
322              [%- LOOP var %]
323                ([% key %])
324              [%- END %]
325
326              Prints
327
328                (a)
329                (b)
330
331       ·   Alloy can parse HTML::Template and HTML::Template::Expr documents
332           as well as TT2 and TT3 documents.
333
334       ·   Added SYNTAX configuration.  The SYNTAX configuration can be used
335           to change what template syntax will be used for parsing included
336           templates or eval'ed strings.
337
338              [% CONFIG SYNTAX => 'hte' %]
339              [% var = '<TMPL_VAR EXPR="sprintf('%s', 'hello world')">' %]
340              [% var | eval %]
341
342       ·   Added @() and $() and CALL_CONTEXT.  Template::Toolkit uses a
343           \concept that Alloy refers to as "smart" context.  All function
344           calls or method calls of variables in Template::Toolkit are made in
345           list context.  If one item is in the list, it is returned.  If two
346           or more items are returned - it returns an arrayref.  This "does
347           the right thing" most of the time - but can cause confusion in some
348           cases and is difficult to work around without writing wrappers for
349           the functions or methods in Perl.
350
351           Alloy has introduced the CALL_CONTEXT configuration item which
352           defaults to "smart," but can also be set to "list" or "item."  List
353           context will always return an arrayref from called functions and
354           methods and will call in list context.  Item context will always
355           call in item (scalar) context and will return one item.
356
357           The @() and $() operators allow for functions embedded inside to
358           use list and item context (respectively).  They are modeled after
359           the corresponding Perl 6 context specifiers.  See the
360           Template::Alloy::Operators perldoc and CALL_CONTEXT configuration
361           documentation for more information.
362
363               [% array = @( this.get_rows ) %]
364
365               [% item  = $( this.get_something ) %]
366
367       ·   Added ->() MACRO operator.
368
369           The ->() operator behaves similarly to the MACRO directive, but can
370           be used to pass functions to map, grep, and sort vmethods.
371
372               [% MACRO foo(n) BLOCK %]Say [% n %][% END %]
373               [% foo = ->(n){ "Say $n" } %]
374
375               [% [0..10].grep(->(this % 2)).join %] prints 3 5 7 9
376               [% ['a' .. 'c'].map(->(a){ a.upper }).join %] prints A B C
377               [% [1,2,3].sort(->(a,b){ b <=> a }).join %] prints 3 2 1
378
379       ·   The RETURN directive can take a variable or expression as a return
380           value.  Their are also "return" list, item, and hash vmethods.
381           Return will also return from an enclosing MACRO.
382
383               [% a = ->(n){ [1..n].return } %]
384
385       ·   Alloy does not generate Perl code.
386
387           It generates an "opcode" tree.  The opcode tree is an arrayref of
388           scalars and array refs nested as deeply as possible.  This "simple"
389           structure could be shared TT implementations in other languages via
390           JSON or YAML.  You can optionally enable generating Perl code by
391           setting COMPILE_PERL = 1.
392
393       ·   Alloy uses storable for its compiled templates.
394
395           If EVAL_PERL is off, Alloy will not eval_string on ANY piece of
396           information.
397
398       ·   There is eval_filter and MACRO recursion protection
399
400           You can control the nested nature of eval_filter and MACRO
401           recursion using the MAX_EVAL_RECURSE and MAX_MACRO_RECURSE
402           configuration items.
403
404       ·   There is no context.
405
406           Alloy provides a context object that mimics the Template::Context
407           interface for use by some TT filters, eval perl blocks, views, and
408           plugins.
409
410       ·   There is no provider.
411
412           Alloy uses the load_template method to get and cache templates.
413
414       ·   There is no parser/grammar.
415
416           Alloy has its own built-in recursive regex based parser and grammar
417           system.
418
419           Alloy can actually be substituted in place of the native
420           Template::Parser and Template::Grammar in TT by using the
421           Template::Parser::Alloy module.  This module uses the output of
422           parse_tree to generate a TT style compiled perl document.
423
424       ·   The DEBUG directive is more limited.
425
426           It only understands DEBUG_DIRS (8) and DEBUG_UNDEF (2).
427
428       ·   Alloy has better line information
429
430           When debug dirs is on, directives on different lines separated by
431           colons show the line they are on rather than a general line range.
432
433           Parse errors actually know what line and character they occurred
434           at.
435

UNSUPPORTED TT2 CONFIGURATION

437       LOAD_TEMPLATES
438           Template::Alloy has its own mechanism for loading and storing
439           compiled templates.  TT would use a Template::Provider that would
440           return a Template::Document.  The closest thing in Template::Alloy
441           is the load_template method.  There is no immediate plan to support
442           the TT behavior.
443
444       LOAD_PLUGINS
445           Template::Alloy uses its own mechanism for loading plugins.  TT
446           would use a Template::Plugins object to load plugins requested via
447           the USE directive.  The functionality for doing this in
448           Template::Alloy is contained in the list_plugins method and the
449           play_USE method.  There is no immediate plan to support the TT
450           behavior.
451
452           Full support is offered for the PLUGINS and LOAD_PERL configuration
453           items.
454
455           Also note that Template::Alloy only has native support for the
456           Iterator plugin.  Any of the other plugins requested will need to
457           provided by installing Template::Toolkit or the appropriate plugin
458           module.
459
460       LOAD_FILTERS
461           Template::Alloy uses its own mechanism for loading filters.  TT
462           would use the Template::Filters object to load filters requested
463           via the FILTER directive.  The functionality for doing this in
464           Template::Alloy is contained in the list_filters method and the
465           play_expr method.
466
467           Full support is offered for the FILTERS configuration item.
468
469       TOLERANT
470           This option is used by the LOAD_TEMPLATES and LOAD_PLUGINS options
471           and is not applicable in Template::Alloy.
472
473       SERVICE
474           Template::Alloy has no concept of service (theoretically the
475           Template::Alloy is the "service").
476
477       CONTEXT
478           Template::Alloy provides its own pseudo context object to plugins,
479           filters, and perl blocks.  The Template::Alloy model doesn't really
480           allow for a separate context.  Template::Alloy IS the context.
481
482       PARSER
483           Template::Alloy has its own built in parser.  The closest
484           similarity is the parse_tree method.  The output of parse_tree is
485           an optree that is later run by execute_tree.  Alloy provides a
486           backend to the Template::Parser::Alloy module which can be used to
487           replace the default parser when using the standard
488           Template::Toolkit library.
489
490       GRAMMAR
491           Template::Alloy maintains its own grammar.  The grammar is defined
492           in the parse_tree method and the callbacks listed in the global
493           $Template::Alloy::Parse::DIRECTIVES hashref.
494

AUTHOR

496       Paul Seamons <paul@seamons.com>
497

LICENSE

499       This module may be distributed under the same terms as Perl itself.
500
501
502
503perl v5.32.1                      2021-03-23            Template::Alloy::TT(3)
Impressum