1Mojo::Template(3)     User Contributed Perl Documentation    Mojo::Template(3)
2
3
4

NAME

6       Mojo::Template - Perlish Templates!
7

SYNOPSIS

9           use Mojo::Template;
10           my $mt = Mojo::Template->new;
11
12           # Simple
13           my $output = $mt->render(<<'EOF');
14           <!doctype html><html>
15               <head><title>Simple</title></head>
16               <body>Time: <%= localtime(time) %></body>
17           </html>
18           EOF
19           print $output;
20
21           # More complicated
22           my $output = $mt->render(<<'EOF', 23, 'foo bar');
23           %= 5 * 5
24           % my ($number, $text) = @_;
25           test 123
26           foo <% my $i = $number + 2; %>
27           % for (1 .. 23) {
28           * some text <%= $i++ %>
29           % }
30           EOF
31           print $output;
32

DESCRIPTION

34       Mojo::Template is a minimalistic and very Perl-ish template engine,
35       designed specifically for all those small tasks that come up during big
36       projects.  Like preprocessing a config file, generating text from
37       heredocs and stuff like that.
38
39           <% Inline Perl %>
40           <%= Perl expression, replaced with result %>
41           <%== Perl expression, replaced with XML escaped result %>
42           <%# Comment, useful for debugging %>
43           % Perl line
44           %= Perl expression line, replaced with result
45           %== Perl expression line, replaced with XML escaped result
46           %# Comment line, useful for debugging
47
48       Automatic escaping behavior can be reversed with the "auto_escape"
49       attribute, this is the default in Mojolicious ".ep" templates for
50       example.
51
52           <%= Perl expression, replaced with XML escaped result %>
53           <%== Perl expression, replaced with result %>
54           %= Perl expression line, replaced with XML escaped result
55           %== Perl expression line, replaced with result
56
57       Mojo::ByteStream objects are always excluded from automatic escaping.
58       Whitespace characters around tags can be trimmed with a special tag
59       ending.
60
61           <%= All whitespace characters around this expression will be trimmed =%>
62
63       You can capture whole template blocks for reuse later.
64
65           <% my $block = {%>
66               <% my $name = shift; =%>
67               Hello <%= $name %>.
68           <%}%>
69           <%= $block->('Sebastian') %>
70           <%= $block->('Sara') %>
71
72           %{ my $block =
73           % my $name = shift;
74           Hello <%= $name %>.
75           %}
76           %= $block->('Baerbel')
77           %= $block->('Wolfgang')
78
79       Mojo::Template templates work just like Perl subs (actually they get
80       compiled to a Perl sub internally).  That means you can access
81       arguments simply via @_.
82
83           % my ($foo, $bar) = @_;
84           % my $x = shift;
85           test 123 <%= $foo %>
86
87       Note that you can't escape Mojo::Template tags, instead we just replace
88       them if necessary.
89
90           my $mt = Mojo::Template->new;
91           $mt->line_start('@@');
92           $mt->tag_start('[@@');
93           $mt->tag_end('@@]');
94           $mt->expression_mark('&');
95           $mt->escape_mark('&');
96           my $output = $mt->render(<<'EOF', 23);
97           @@ my $i = shift;
98           <% no code just text [@@&& $i @@]
99           EOF
100
101       There is only one case that we can escape with a backslash, and thats a
102       newline at the end of a template line.
103
104          This is <%= 23 * 3 %> a\
105          single line
106
107       If for some strange reason you absolutely need a backslash in front of
108       a newline you can escape the backslash with another backslash.
109
110           % use Data::Dumper;
111           This will\\
112           result <%=  Dumper {foo => 'bar'} %>\\
113           in multiple lines
114
115       Templates get compiled to Perl code internally, this can make debugging
116       a bit tricky.  But Mojo::Template will return Mojo::Exception objects
117       that stringify to error messages with context.
118
119           Bareword "xx" not allowed while "strict subs" in use at template line 4.
120           2: </head>
121           3: <body>
122           4: % my $i = 2; xx
123           5: %= $i * 2
124           6: </body>
125
126       Mojo::Template does not support caching by itself, but you can easily
127       build a wrapper around it.
128
129           # Compile and store code somewhere
130           my $mt = Mojo::Template->new;
131           $mt->parse($template);
132           $mt->build;
133           my $code = $mt->code;
134
135           # Load code and template (template for debug trace only)
136           $mt->template($template);
137           $mt->code($code);
138           $mt->compile;
139           my $output = $mt->interpret(@arguments);
140

ATTRIBUTES

142       Mojo::Template implements the following attributes.
143
144   "auto_escape"
145           my $auto_escape = $mt->auto_escape;
146           $mt             = $mt->auto_escape(1);
147
148       Activate automatic XML escaping.
149
150   "append"
151           my $code = $mt->append;
152           $mt      = $mt->append('warn "Processed template"');
153
154       Append Perl code to compiled template.
155
156   "capture_end"
157           my $capture_end = $mt->capture_end;
158           $mt             = $mt->capture_end('}');
159
160       Character indicating the end of a capture block, defaults to "}".
161
162           %{ $block =
163               Some data!
164           %}
165
166   "capture_start"
167           my $capture_start = $mt->capture_start;
168           $mt               = $mt->capture_start('{');
169
170       Character indicating the start of a capture block, defaults to "{".
171
172           <% my $block = {%>
173               Some data!
174           <%}%>
175
176   "code"
177           my $code = $mt->code;
178           $mt      = $mt->code($code);
179
180       Compiled template code.
181
182   "comment_mark"
183           my $comment_mark = $mt->comment_mark;
184           $mt              = $mt->comment_mark('#');
185
186       Character indicating the start of a comment, defaults to "#".
187
188           <%# This is a comment %>
189
190   "encoding"
191           my $encoding = $mt->encoding;
192           $mt          = $mt->encoding('UTF-8');
193
194       Encoding used for template files.
195
196   "escape_mark"
197           my $escape_mark = $mt->escape_mark;
198           $mt             = $mt->escape_mark('=');
199
200       Character indicating the start of an escaped expression, defaults to
201       "=".
202
203           <%== $foo %>
204
205   "expression_mark"
206           my $expression_mark = $mt->expression_mark;
207           $mt                 = $mt->expression_mark('=');
208
209       Character indicating the start of an expression, defaults to "=".
210
211           <%= $foo %>
212
213   "line_start"
214           my $line_start = $mt->line_start;
215           $mt            = $mt->line_start('%');
216
217       Character indicating the start of a code line, defaults to "%".
218
219           % $foo = 23;
220
221   "namespace"
222           my $namespace = $mt->namespace;
223           $mt           = $mt->namespace('main');
224
225       Namespace used to compile templates.
226
227   "prepend"
228           my $code = $mt->prepend;
229           $mt      = $mt->prepend('my $self = shift;');
230
231       Prepend Perl code to compiled template.
232
233   "tag_start"
234           my $tag_start = $mt->tag_start;
235           $mt           = $mt->tag_start('<%');
236
237       Characters indicating the start of a tag, defaults to "<%".
238
239           <% $foo = 23; %>
240
241   "tag_end"
242           my $tag_end = $mt->tag_end;
243           $mt         = $mt->tag_end('%>');
244
245       Characters indicating the end of a tag, defaults to "%>".
246
247           <%= $foo %>
248
249   "template"
250           my $template = $mt->template;
251           $mt          = $mt->template($template);
252
253       Raw template.
254
255   "tree"
256           my $tree = $mt->tree;
257           $mt      = $mt->tree($tree);
258
259       Parsed tree.
260
261   "trim_mark"
262           my $trim_mark = $mt->trim_mark;
263           $mt           = $mt->trim_mark('-');
264
265       Character activating automatic whitespace trimming, defaults to "=".
266
267           <%= $foo =%>
268

METHODS

270       Mojo::Template inherits all methods from Mojo::Base and implements the
271       following new ones.
272
273   "new"
274           my $mt = Mojo::Template->new;
275
276       Construct a new Mojo::Template object.
277
278   "build"
279           $mt = $mt->build;
280
281       Build template.
282
283   "compile"
284           my $exception = $mt->compile;
285
286       Compile template.
287
288   "interpret"
289           my $output = $mt->interpret;
290           my $output = $mt->interpret(@arguments);
291
292       Interpret template.
293
294   "parse"
295           $mt = $mt->parse($template);
296
297       Parse template.
298
299   "render"
300           my $output = $mt->render($template);
301           my $output = $mt->render($template, @arguments);
302
303       Render template.
304
305   "render_file"
306           my $output = $mt->render_file($template_file);
307           my $output = $mt->render_file($template_file, @arguments);
308
309       Render template file.
310
311   "render_file_to_file"
312           my $exception = $mt->render_file_to_file($template_file, $output_file);
313           my $exception = $mt->render_file_to_file(
314               $template_file, $output_file, @arguments
315           );
316
317       Render template file to a specific file.
318
319   "render_to_file"
320           my $exception = $mt->render_to_file($template, $output_file);
321           my $exception = $mt->render_to_file(
322               $template, $output_file, @arguments
323           );
324
325       Render template to a specific file.
326

SEE ALSO

328       Mojolicious, Mojolicious::Guides, <http://mojolicious.org>.
329
330
331
332perl v5.12.3                      2010-08-12                 Mojo::Template(3)
Impressum