.\" Automatically generated by Pod::Man 2.25 (Pod::Simple 3.16)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings. \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote. \*(C+ will
.\" give a nicer C++. Capital omega is used to do unbreakable dashes and
.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
. ds -- \(*W-
. ds PI pi
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
. ds L" ""
. ds R" ""
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds -- \|\(em\|
. ds PI \(*p
. ds L" ``
. ds R" ''
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.ie \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. nr % 0
. rr F
.\}
.el \{\
. de IX
..
.\}
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear. Run. Save yourself. No user-serviceable parts.
. \" fudge factors for nroff and troff
.if n \{\
. ds #H 0
. ds #V .8m
. ds #F .3m
. ds #[ \f1
. ds #] \fP
.\}
.if t \{\
. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
. ds #V .6m
. ds #F 0
. ds #[ \&
. ds #] \&
.\}
. \" simple accents for nroff and troff
.if n \{\
. ds ' \&
. ds ` \&
. ds ^ \&
. ds , \&
. ds ~ ~
. ds /
.\}
.if t \{\
. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
. \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
. \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
. \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
. ds : e
. ds 8 ss
. ds o a
. ds d- d\h'-1'\(ga
. ds D- D\h'-1'\(hy
. ds th \o'bp'
. ds Th \o'LP'
. ds ae ae
. ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "Mojo::Template 3pm"
.TH Mojo::Template 3pm "2012-09-05" "perl v5.14.2" "User Contributed Perl Documentation"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
Mojo::Template \- Perl\-ish templates!
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 2
\& use Mojo::Template;
\& my $mt = Mojo::Template\->new;
\&
\& # Simple
\& my $output = $mt\->render(<<\*(AqEOF\*(Aq);
\& % use Time::Piece;
\&
\&
\&
Simple
\& % my $now = localtime;
\& Time: <%= $now\->hms %>
\&
\& EOF
\& say $output;
\&
\& # More advanced
\& my $output = $mt\->render(<<\*(AqEOF\*(Aq, 23, \*(Aqfoo bar\*(Aq);
\& % my ($number, $text) = @_;
\& %= 5 * 5
\&
\&
\& More advanced
\&
\& test 123
\& foo <% my $i = $number + 2; %>
\& % for (1 .. 23) {
\& * some text <%= $i++ %>
\& % }
\&
\&
\& EOF
\& say $output;
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
Mojo::Template is a minimalistic and very Perl-ish template engine,
designed specifically for all those small tasks that come up during big
projects. Like preprocessing a configuration file, generating text from
heredocs and stuff like that.
.PP
.Vb 10
\& <% Perl code %>
\& <%= Perl expression, replaced with result %>
\& <%== Perl expression, replaced with XML escaped result %>
\& <%# Comment, useful for debugging %>
\& <%% Replaced with "<%", useful for generating templates %>
\& % Perl code line, treated as "<% line =%>"
\& %= Perl expression line, treated as "<%= line %>"
\& %== Perl expression line, treated as "<%== line %>"
\& %# Comment line, treated as "<%# line =%>"
\& %% Replaced with "%", useful for generating templates
.Ve
.PP
Escaping behavior can be reversed with the \f(CW\*(C`auto_escape\*(C'\fR attribute, this is
the default in Mojolicious \f(CW\*(C`.ep\*(C'\fR templates for example.
.PP
.Vb 2
\& <%= Perl expression, replaced with XML escaped result %>
\& <%== Perl expression, replaced with result %>
.Ve
.PP
Mojo::ByteStream objects are always excluded from automatic escaping.
.PP
.Vb 1
\& <%= Mojo::ByteStream\->new(\*(Aqexcluded!
\*(Aq) %>
.Ve
.PP
Newlines can be escaped with a backslash.
.PP
.Vb 2
\& This is <%= 1 + 1 %> a\e
\& single line
.Ve
.PP
And a backslash in front of a newline can be escaped with another backslash.
.PP
.Vb 3
\& This will <%= 1 + 1 %> result\e\e
\& in multiple\e\e
\& lines
.Ve
.PP
Whitespace characters around tags can be trimmed with a special tag ending.
.PP
.Vb 1
\& <%= All whitespace characters around this expression will be trimmed =%>
.Ve
.PP
You can capture whole template blocks for reuse later with the \f(CW\*(C`begin\*(C'\fR and
\&\f(CW\*(C`end\*(C'\fR keywords.
.PP
.Vb 6
\& <% my $block = begin %>
\& <% my $name = shift; =%>
\& Hello <%= $name %>.
\& <% end %>
\& <%= $block\->(\*(AqBaerbel\*(Aq) %>
\& <%= $block\->(\*(AqWolfgang\*(Aq) %>
.Ve
.PP
Perl lines can also be indented freely.
.PP
.Vb 6
\& % my $block = begin
\& % my $name = shift;
\& Hello <%= $name %>.
\& % end
\& %= $block\->(\*(AqBaerbel\*(Aq)
\& %= $block\->(\*(AqWolfgang\*(Aq)
.Ve
.PP
Mojo::Template templates work just like Perl subs (actually they get
compiled to a Perl sub internally). That means you can access arguments simply
via \f(CW@_\fR.
.PP
.Vb 3
\& % my ($foo, $bar) = @_;
\& % my $x = shift;
\& test 123 <%= $foo %>
.Ve
.PP
Templates get compiled to Perl code internally, this can make debugging a bit
tricky. But Mojo::Template will return Mojo::Exception objects that
stringify to error messages with context.
.PP
.Vb 6
\& Bareword "xx" not allowed while "strict subs" in use at template line 4.
\& 2:
\& 3:
\& 4: % my $i = 2; xx
\& 5: %= $i * 2
\& 6:
.Ve
.PP
See Mojolicious::Guides::Rendering for more.
.SH "ATTRIBUTES"
.IX Header "ATTRIBUTES"
Mojo::Template implements the following attributes.
.ie n .SS """auto_escape"""
.el .SS "\f(CWauto_escape\fP"
.IX Subsection "auto_escape"
.Vb 2
\& my $auto_escape = $mt\->auto_escape;
\& $mt = $mt\->auto_escape(1);
.Ve
.PP
Activate automatic \s-1XML\s0 escaping.
.ie n .SS """append"""
.el .SS "\f(CWappend\fP"
.IX Subsection "append"
.Vb 2
\& my $code = $mt\->append;
\& $mt = $mt\->append(\*(Aqwarn "Processed template"\*(Aq);
.Ve
.PP
Append Perl code to compiled template.
.ie n .SS """capture_end"""
.el .SS "\f(CWcapture_end\fP"
.IX Subsection "capture_end"
.Vb 2
\& my $capture_end = $mt\->capture_end;
\& $mt = $mt\->capture_end(\*(Aqend\*(Aq);
.Ve
.PP
Keyword indicating the end of a capture block, defaults to \f(CW\*(C`end\*(C'\fR.
.PP
.Vb 3
\& <% my $block = begin %>
\& Some data!
\& <% end %>
.Ve
.ie n .SS """capture_start"""
.el .SS "\f(CWcapture_start\fP"
.IX Subsection "capture_start"
.Vb 2
\& my $capture_start = $mt\->capture_start;
\& $mt = $mt\->capture_start(\*(Aqbegin\*(Aq);
.Ve
.PP
Keyword indicating the start of a capture block, defaults to \f(CW\*(C`begin\*(C'\fR.
.PP
.Vb 3
\& <% my $block = begin %>
\& Some data!
\& <% end %>
.Ve
.ie n .SS """code"""
.el .SS "\f(CWcode\fP"
.IX Subsection "code"
.Vb 2
\& my $code = $mt\->code;
\& $mt = $mt\->code($code);
.Ve
.PP
Compiled template code.
.ie n .SS """comment_mark"""
.el .SS "\f(CWcomment_mark\fP"
.IX Subsection "comment_mark"
.Vb 2
\& my $comment_mark = $mt\->comment_mark;
\& $mt = $mt\->comment_mark(\*(Aq#\*(Aq);
.Ve
.PP
Character indicating the start of a comment, defaults to \f(CW\*(C`#\*(C'\fR.
.PP
.Vb 1
\& <%# This is a comment %>
.Ve
.ie n .SS """encoding"""
.el .SS "\f(CWencoding\fP"
.IX Subsection "encoding"
.Vb 2
\& my $encoding = $mt\->encoding;
\& $mt = $mt\->encoding(\*(AqUTF\-8\*(Aq);
.Ve
.PP
Encoding used for template files.
.ie n .SS """escape_mark"""
.el .SS "\f(CWescape_mark\fP"
.IX Subsection "escape_mark"
.Vb 2
\& my $escape_mark = $mt\->escape_mark;
\& $mt = $mt\->escape_mark(\*(Aq=\*(Aq);
.Ve
.PP
Character indicating the start of an escaped expression, defaults to \f(CW\*(C`=\*(C'\fR.
.PP
.Vb 1
\& <%== $foo %>
.Ve
.ie n .SS """expression_mark"""
.el .SS "\f(CWexpression_mark\fP"
.IX Subsection "expression_mark"
.Vb 2
\& my $expression_mark = $mt\->expression_mark;
\& $mt = $mt\->expression_mark(\*(Aq=\*(Aq);
.Ve
.PP
Character indicating the start of an expression, defaults to \f(CW\*(C`=\*(C'\fR.
.PP
.Vb 1
\& <%= $foo %>
.Ve
.ie n .SS """line_start"""
.el .SS "\f(CWline_start\fP"
.IX Subsection "line_start"
.Vb 2
\& my $line_start = $mt\->line_start;
\& $mt = $mt\->line_start(\*(Aq%\*(Aq);
.Ve
.PP
Character indicating the start of a code line, defaults to \f(CW\*(C`%\*(C'\fR.
.PP
.Vb 1
\& % $foo = 23;
.Ve
.ie n .SS """name"""
.el .SS "\f(CWname\fP"
.IX Subsection "name"
.Vb 2
\& my $name = $mt\->name;
\& $mt = $mt\->name(\*(Aqfoo.mt\*(Aq);
.Ve
.PP
Name of template currently being processed, defaults to \f(CW\*(C`template\*(C'\fR. Note that
this method is attribute and might change without warning!
.ie n .SS """namespace"""
.el .SS "\f(CWnamespace\fP"
.IX Subsection "namespace"
.Vb 2
\& my $namespace = $mt\->namespace;
\& $mt = $mt\->namespace(\*(Aqmain\*(Aq);
.Ve
.PP
Namespace used to compile templates, defaults to \f(CW\*(C`Mojo::Template::SandBox\*(C'\fR.
.ie n .SS """prepend"""
.el .SS "\f(CWprepend\fP"
.IX Subsection "prepend"
.Vb 2
\& my $code = $mt\->prepend;
\& $mt = $mt\->prepend(\*(Aqmy $self = shift;\*(Aq);
.Ve
.PP
Prepend Perl code to compiled template.
.ie n .SS """replace_mark"""
.el .SS "\f(CWreplace_mark\fP"
.IX Subsection "replace_mark"
.Vb 2
\& my $replace_mark = $mt\->replace_mark;
\& $mt = $mt\->replace_mark(\*(Aq%\*(Aq);
.Ve
.PP
Character used for escaping the start of a tag or line, defaults to \f(CW\*(C`%\*(C'\fR.
.PP
.Vb 1
\& <%% my $foo = 23; %>
.Ve
.ie n .SS """tag_start"""
.el .SS "\f(CWtag_start\fP"
.IX Subsection "tag_start"
.Vb 2
\& my $tag_start = $mt\->tag_start;
\& $mt = $mt\->tag_start(\*(Aq<%\*(Aq);
.Ve
.PP
Characters indicating the start of a tag, defaults to \f(CW\*(C`<%\*(C'\fR.
.PP
.Vb 1
\& <% $foo = 23; %>
.Ve
.ie n .SS """tag_end"""
.el .SS "\f(CWtag_end\fP"
.IX Subsection "tag_end"
.Vb 2
\& my $tag_end = $mt\->tag_end;
\& $mt = $mt\->tag_end(\*(Aq%>\*(Aq);
.Ve
.PP
Characters indicating the end of a tag, defaults to \f(CW\*(C`%>\*(C'\fR.
.PP
.Vb 1
\& <%= $foo %>
.Ve
.ie n .SS """template"""
.el .SS "\f(CWtemplate\fP"
.IX Subsection "template"
.Vb 2
\& my $template = $mt\->template;
\& $mt = $mt\->template($template);
.Ve
.PP
Raw template.
.ie n .SS """tree"""
.el .SS "\f(CWtree\fP"
.IX Subsection "tree"
.Vb 2
\& my $tree = $mt\->tree;
\& $mt = $mt\->tree($tree);
.Ve
.PP
Parsed tree.
.ie n .SS """trim_mark"""
.el .SS "\f(CWtrim_mark\fP"
.IX Subsection "trim_mark"
.Vb 2
\& my $trim_mark = $mt\->trim_mark;
\& $mt = $mt\->trim_mark(\*(Aq\-\*(Aq);
.Ve
.PP
Character activating automatic whitespace trimming, defaults to \f(CW\*(C`=\*(C'\fR.
.PP
.Vb 1
\& <%= $foo =%>
.Ve
.SH "METHODS"
.IX Header "METHODS"
Mojo::Template inherits all methods from Mojo::Base and implements the
following new ones.
.ie n .SS """new"""
.el .SS "\f(CWnew\fP"
.IX Subsection "new"
.Vb 1
\& my $mt = Mojo::Template\->new;
.Ve
.PP
Construct a new Mojo::Template object.
.ie n .SS """build"""
.el .SS "\f(CWbuild\fP"
.IX Subsection "build"
.Vb 1
\& $mt = $mt\->build;
.Ve
.PP
Build template.
.ie n .SS """compile"""
.el .SS "\f(CWcompile\fP"
.IX Subsection "compile"
.Vb 1
\& my $exception = $mt\->compile;
.Ve
.PP
Compile template.
.ie n .SS """interpret"""
.el .SS "\f(CWinterpret\fP"
.IX Subsection "interpret"
.Vb 2
\& my $output = $mt\->interpret;
\& my $output = $mt\->interpret(@args);
.Ve
.PP
Interpret template.
.PP
.Vb 4
\& # Reuse template
\& say $mt\->render(\*(AqHello <%= $_[0] %>!\*(Aq, \*(AqBender\*(Aq);
\& say $mt\->interpret(\*(AqFry\*(Aq);
\& say $mt\->interpret(\*(AqLeela\*(Aq);
.Ve
.ie n .SS """parse"""
.el .SS "\f(CWparse\fP"
.IX Subsection "parse"
.Vb 1
\& $mt = $mt\->parse($template);
.Ve
.PP
Parse template.
.ie n .SS """render"""
.el .SS "\f(CWrender\fP"
.IX Subsection "render"
.Vb 2
\& my $output = $mt\->render($template);
\& my $output = $mt\->render($template, @args);
.Ve
.PP
Render template.
.PP
.Vb 1
\& say $mt\->render(\*(AqHello <%= $_[0] %>!\*(Aq, \*(AqBender\*(Aq);
.Ve
.ie n .SS """render_file"""
.el .SS "\f(CWrender_file\fP"
.IX Subsection "render_file"
.Vb 2
\& my $output = $mt\->render_file(\*(Aq/tmp/foo.mt\*(Aq);
\& my $output = $mt\->render_file(\*(Aq/tmp/foo.mt\*(Aq, @args);
.Ve
.PP
Render template file.
.ie n .SS """render_file_to_file"""
.el .SS "\f(CWrender_file_to_file\fP"
.IX Subsection "render_file_to_file"
.Vb 3
\& my $exception = $mt\->render_file_to_file(\*(Aq/tmp/foo.mt\*(Aq, \*(Aq/tmp/foo.txt\*(Aq);
\& my $exception
\& = $mt\->render_file_to_file(\*(Aq/tmp/foo.mt\*(Aq, \*(Aq/tmp/foo.txt\*(Aq, @args);
.Ve
.PP
Render template file to another file.
.ie n .SS """render_to_file"""
.el .SS "\f(CWrender_to_file\fP"
.IX Subsection "render_to_file"
.Vb 2
\& my $exception = $mt\->render_to_file($template, \*(Aq/tmp/foo.txt\*(Aq);
\& my $exception = $mt\->render_to_file($template, \*(Aq/tmp/foo.txt\*(Aq, @args);
.Ve
.PP
Render template to a file.
.SH "SEE ALSO"
.IX Header "SEE ALSO"
Mojolicious, Mojolicious::Guides, .