1Template::Alloy::TT(3)User Contributed Perl DocumentationTemplate::Alloy::TT(3)
2
3
4
6 Template::Alloy::TT - Template::Toolkit role
7
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 fea‐
11 tures 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
19 Alloy uses the same base template syntax and configuration items as
20 TT2, but the internals of Alloy were written from scratch. Addition‐
21 ally much of the planned TT3 syntax is supported as well as most of
22 that of HTML::Template::Expr. The following is a list of some of the
23 ways that the configuration and syntax of Alloy are different from that
24 of TT2. Note: items that are planned to work in TT3 are marked with
25 (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 syn‐
118 taxes 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 meth‐
142 ods.
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 con‐
189 figuration 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 hexidecimal 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 [% a = [[1..3], [5..7]] %][% i FOREACH i = j FOREACH j = a %] # = 123567
267
268 · Semi-colons on directives in the same tag are optional. (TT3)
269
270 [% SET a = 1
271 GET a
272 %]
273
274 [% FOREACH i = [1 .. 10]
275 i
276 END %]
277
278 Note: a semi-colon is still required in front of any block direc‐
279 tive that can be used as a post-operative directive.
280
281 [% 1 IF 0
282 2 %] # prints 2
283
284 [% 1; IF 0
285 2
286 END %] # prints 1
287
288 Note2: This behavior can be disabled by setting the SEMICOLONS con‐
289 figuration item to a true value. If SEMICOLONS is true, then a
290 SEMICOLON must be set after any directive that isn't followed by a
291 post-operative directive.
292
293 · CATCH blocks can be empty.
294
295 TT2 requires them to contain something.
296
297 · Added a DUMP directive.
298
299 Used for Data::Dumpering the passed variable or expression.
300
301 [% DUMP a.a %]
302
303 · Added CONFIG directive.
304
305 [% CONFIG
306 ANYCASE => 1
307 PRE_CHOMP => '-'
308 %]
309
310 · Configuration options can use lowercase names instead of the all
311 uppercase names that TT2 uses.
312
313 my $t = Template::Alloy->new({
314 anycase => 1,
315 interpolate => 1,
316 });
317
318 · Added LOOP directive (works the same as LOOP in HTML::Template.
319
320 [%- var = [{key => 'a'}, {key => 'b'}] %]
321 [%- LOOP var %]
322 ([% key %])
323 [%- END %]
324
325 Prints
326
327 (a)
328 (b)
329
330 · Alloy can parse HTML::Template and HTML::Template::Expr documents
331 as well as TT2 and TT3 documents.
332
333 · Added SYNTAX configuration. The SYNTAX configuration can be used
334 to change what template syntax will be used for parsing included
335 templates or eval'ed strings.
336
337 [% CONFIG SYNTAX => 'hte' %]
338 [% var = '<TMPL_VAR EXPR="sprintf('%s', 'hello world')">' %]
339 [% var ⎪ eval %]
340
341 · Alloy does not generate Perl code.
342
343 It generates an "opcode" tree. The opcode tree is an arrayref of
344 scalars and array refs nested as deeply as possible. This "simple"
345 structure could be shared TT implementations in other languages via
346 JSON or YAML.
347
348 · Alloy uses storable for its compiled templates.
349
350 If EVAL_PERL is off, Alloy will not eval_string on ANY piece of
351 information.
352
353 · There is eval_filter and MACRO recursion protection
354
355 You can control the nested nature of eval_filter and MACRO recur‐
356 sion using the MAX_EVAL_RECURSE and MAX_MACRO_RECURSE configuration
357 items.
358
359 · There is no context.
360
361 Alloy provides a context object that mimics the Template::Context
362 interface for use by some TT filters, eval perl blocks, views, and
363 plugins.
364
365 · There is no provider.
366
367 Alloy uses the load_template method to get and cache templates.
368
369 · There is no parser/grammar.
370
371 Alloy has its own built-in recursive regex based parser and grammar
372 system.
373
374 Alloy can actually be substituted in place of the native Tem‐
375 plate::Parser and Template::Grammar in TT by using the Tem‐
376 plate::Parser::Alloy module. This module uses the output of
377 parse_tree to generate a TT style compiled perl document.
378
379 · The DEBUG directive is more limited.
380
381 It only understands DEBUG_DIRS (8) and DEBUG_UNDEF (2).
382
383 · Alloy has better line information
384
385 When debug dirs is on, directives on different lines separated by
386 colons show the line they are on rather than a general line range.
387
388 Parse errors actually know what line and character they occured at.
389
391 LOAD_TEMPLATES
392 Template::Alloy has its own mechanism for loading and storing com‐
393 piled templates. TT would use a Template::Provider that would
394 return a Template::Document. The closest thing in Template::Alloy
395 is the load_template method. There is no immediate plan to support
396 the TT behavior.
397
398 LOAD_PLUGINS
399 Template::Alloy uses its own mechanism for loading plugins. TT
400 would use a Template::Plugins object to load plugins requested via
401 the USE directive. The functionality for doing this in Tem‐
402 plate::Alloy is contained in the list_plugins method and the
403 play_USE method. There is no immediate plan to support the TT
404 behavior.
405
406 Full support is offered for the PLUGINS and LOAD_PERL configuration
407 items.
408
409 Also note that Template::Alloy only natively supports the Iterator
410 plugin. Any of the other plugins requested will need to provided
411 by installing Template::Toolkit or the appropriate plugin module.
412
413 LOAD_FILTERS
414 Template::Alloy uses its own mechanism for loading filters. TT
415 would use the Template::Filters object to load filters requested
416 via the FILTER directive. The functionality for doing this in Tem‐
417 plate::Alloy is contained in the list_filters method and the
418 play_expr method.
419
420 Full support is offered for the FILTERS configuration item.
421
422 TOLERANT
423 This option is used by the LOAD_TEMPLATES and LOAD_PLUGINS options
424 and is not applicable in Template::Alloy.
425
426 SERVICE
427 Template::Alloy has no concept of service (theoretically the Tem‐
428 plate::Alloy is the "service").
429
430 CONTEXT
431 Template::Alloy provides its own pseudo context object to plugins,
432 filters, and perl blocks. The Template::Alloy model doesn't really
433 allow for a separate context. Template::Alloy IS the context.
434
435 PARSER
436 Template::Alloy has its own built in parser. The closest similar‐
437 ity is the parse_tree method. The output of parse_tree is an
438 optree that is later run by execute_tree. Alloy provides a backend
439 to the Template::Parser::Alloy module which can be used to replace
440 the default parser when using the standard Template::Toolkit
441 library.
442
443 GRAMMAR
444 Template::Alloy maintains its own grammar. The grammar is defined
445 in the parse_tree method and the callbacks listed in the global
446 $Template::Alloy::Parse::DIRECTIVES hashref.
447
449 Paul Seamons <perl at seamons dot com>
450
452 This module may be distributed under the same terms as Perl itself.
453
454
455
456perl v5.8.8 2007-07-24 Template::Alloy::TT(3)