1Template::Tiny(3) User Contributed Perl Documentation Template::Tiny(3)
2
3
4
6 Template::Tiny - Template Toolkit reimplemented in as little code as
7 possible
8
10 version 1.14
11
13 my $template = Template::Tiny->new(
14 TRIM => 1,
15 );
16
17 # Print the template results to STDOUT
18 $template->process( <<'END_TEMPLATE', { foo => 'World' } );
19 Hello [% foo %]!
20 END_TEMPLATE
21
23 Template::Tiny is a reimplementation of a subset of the functionality
24 from Template Toolkit in as few lines of code as possible.
25
26 It is intended for use in light-usage, low-memory, or low-cpu
27 templating situations, where you may need to upgrade to the full
28 feature set in the future, or if you want the retain the familiarity of
29 TT-style templates.
30
31 For the subset of functionality it implements, it has fully-compatible
32 template and stash API. All templates used with Template::Tiny should
33 be able to be transparently upgraded to full Template Toolkit.
34
35 Unlike Template Toolkit, Template::Tiny will process templates without
36 a compile phase (but despite this is still quicker, owing to heavy use
37 of the Perl regular expression engine.
38
39 SUPPORTED USAGE
40 Only the default "[% %]" tag style is supported.
41
42 Both the "[%+ +%]" style explicit whitespace and the "[%- -%]" style
43 explicit chomp are support, although the "[%+ +%]" version is unneeded
44 in practice as Template::Tiny does not support default-enabled
45 "PRE_CHOMP" or "POST_CHOMP".
46
47 Variable expressions in the form "[% foo.bar.baz %]" are supported.
48
49 Appropriate simple behaviours for "ARRAY" references, "HASH" references
50 and objects are supported. "VMethods" such as [% array.length %] are
51 not supported at this time.
52
53 "IF", "ELSE" and "UNLESS" conditional blocks are supported, but only
54 with simple "[% foo.bar.baz %]" conditions.
55
56 Support for looping (or rather iteration) is available in simple "[%
57 FOREACH item IN list %]" form is supported. Other loop structures are
58 not supported. Because support for arbitrary or infinite looping is not
59 available, Template::Tiny templates are not turing complete. This is
60 intentional.
61
62 All of the four supported control structures
63 "IF"/"ELSE"/"UNLESS"/"FOREACH" can be nested to arbitrary depth.
64
65 The treatment of "_private" hash and method keys is compatible with
66 Template Toolkit, returning null or false rather than the actual
67 content of the hash key or method.
68
69 Anything beyond the above is currently out of scope.
70
72 new
73 my $template = Template::Tiny->new(
74 TRIM => 1,
75 );
76
77 The "new" constructor is provided for compatibility with Template
78 Toolkit.
79
80 The only parameter it currently supports is "TRIM" (which removes
81 leading and trailing whitespace from processed templates).
82
83 Additional parameters can be provided without error, but will be
84 ignored.
85
86 process
87 # DEPRECATED: Return template results (emits a warning)
88 my $text = $template->process( \$input, $vars );
89
90 # Print template results to STDOUT
91 $template->process( \$input, $vars );
92
93 # Generate template results into a variable
94 my $output = '';
95 $template->process( \$input, $vars, \$output );
96
97 The "process" method is called to process a template.
98
99 The first parameter is a reference to a text string containing the
100 template text. A reference to a hash may be passed as the second
101 parameter containing definitions of template variables.
102
103 If a third parameter is provided, it must be a scalar reference to be
104 populated with the output of the template.
105
106 For a limited amount of time, the old deprecated interface will
107 continue to be supported. If "process" is called without a third
108 parameter, and in scalar or list contest, the template results will be
109 returned to the caller.
110
111 If "process" is called without a third parameter, and in void context,
112 the template results will be print()ed to the currently selected file
113 handle (probably "STDOUT") for compatibility with Template.
114
116 Template::Tiny::Strict, Config::Tiny, CSS::Tiny, YAML::Tiny
117
119 Bugs may be submitted through the RT bug tracker
120 <https://rt.cpan.org/Public/Dist/Display.html?Name=Template-Tiny> (or
121 bug-Template-Tiny@rt.cpan.org <mailto:bug-Template-Tiny@rt.cpan.org>).
122
124 Adam Kennedy <adamk@cpan.org>
125
127 • Adam Kennedy <adam@ali.as>
128
129 • Karen Etheridge <ether@cpan.org>
130
131 • Alexandr Ciornii <alexchorny@gmail.com>
132
133 • Matt S Trout <mst@shadowcat.co.uk>
134
136 This software is copyright (c) 2009 by Adam Kennedy.
137
138 This is free software; you can redistribute it and/or modify it under
139 the same terms as the Perl 5 programming language system itself.
140
141
142
143perl v5.38.0 2023-07-21 Template::Tiny(3)