.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" 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 .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' '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 >0, 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. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Text::Xslate::Syntax::TTerse 3pm" .TH Text::Xslate::Syntax::TTerse 3pm 2024-03-07 "perl v5.38.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 Text::Xslate::Syntax::TTerse \- An alternative syntax compatible with Template Toolkit 2 .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 4 \& use Text::Xslate; \& my $tx = Text::Xslate\->new( \& syntax => \*(AqTTerse\*(Aq, \& ); \& \& print $tx\->render_string( \& \*(AqHello, [% dialect %] world!\*(Aq, \& { dialect => \*(AqTTerse\*(Aq } \& ); \& \& # PRE_PROCESS/POST_PROCESS \& $tx = Text::Xslate\->new( \& syntax => \*(AqTTerse\*(Aq, \& header => [\*(Aqheader.tt\*(Aq], \& footer => [\*(Aqfooter.tt\*(Aq], \& ); .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" TTerse is a subset of the Template-Toolkit 2 (and partially 3) syntax, using \f(CW\*(C`[% ... %]\*(C'\fR tags and \f(CW\*(C`%% ...\*(C'\fR line code. .PP Note that TTerse itself has few methods and filters while Template-Toolkit 2 has a lot. See \f(CW\*(C`Text::Xslate::Bridge::*\*(C'\fR modules on CPAN which provide extra methods and filters if you want to use those features. .PP (TODO: I should concentrate on the difference between Template-Toolkit 2 and TTerse) .SH SYNTAX .IX Header "SYNTAX" This supports a Template-Toolkit compatible syntax, although the details might be different. .PP Note that lower-cased keywords, which are inspired in Template-Toolkit 3, are also allowed. .SS "Variable access" .IX Subsection "Variable access" Scalar access: .PP .Vb 3 \& [% var %] \& [% $var %] \& [% GET var # \*(AqGET\*(Aq is optional %] .Ve .PP Field access: .PP .Vb 5 \& [% var.0 %] \& [% var.field %] \& [% var.accessor %] \& [% var.$field ]% \& [% var[$field] # TTerse specific %] .Ve .PP Variables may be HASH references, ARRAY references, or objects. .PP If \fR\f(CI$var\fR\fI\fR is an object instance, you can call its methods. .PP .Vb 4 \& [% $var.method() %] \& [% $var.method(1, 2, 3) %] \& [% $var.method(foo => [1, 2, 3]) %] \& [% $var.method({ foo => \*(Aqbar\*(Aq }) %] .Ve .SS Expressions .IX Subsection "Expressions" Almost the same as Text::Xslate::Syntax::Kolon, but \f(CW\*(C`infix:<_>\*(C'\fR for concatenation is supported for compatibility. .SS Loops .IX Subsection "Loops" .Vb 3 \& [% FOREACH item IN arrayref %] \& * [% item %] \& [% END %] .Ve .PP Loop iterators are partially supported. .PP .Vb 10 \& [% FOREACH item IN arrayref %] \& [%\- IF loop.is_first \-%] \& \& [%\- END \-%] \& * [% loop.index %] # 0 origin \& * [% loop.count # loop.index + 1 %] \& * [% loop.body # alias to arrayref %] \& * [% loop.size # loop.body.size %] \& * [% loop.max_index # loop.size \- 1 %] \& * [% loop.peek_next # loop.body[ loop.index + 1 %] \& * [% loop.peek_prev # loop.body[ loop.index \- 1 %] \& [%\- IF loop.is_last \-%] \& \& [%\- END \-%] \& [% END %] .Ve .PP Unlike Template-Toolkit, \f(CW\*(C`FOREACH\*(C'\fR doesn't accept a HASH reference, so you must convert HASH references to ARRAY references by \f(CWkeys()\fR, \f(CWvalues()\fR, or \f(CWkv()\fR methods. .PP Template-Toolkit compatible names are also supported, but the use of them is discouraged because they are not easy to understand: .PP .Vb 5 \& loop.max # for loop.max_index \& loop.next # for loop.peek_next \& loop.prev # for loop.peek_prev \& loop.first # for loop.is_first \& loop.last # for loop.is_last .Ve .PP Loop control statements, namely \f(CW\*(C`NEXT\*(C'\fR and \f(CW\*(C`LAST\*(C'\fR, are also supported in both \f(CW\*(C`FOR\*(C'\fR and \f(CW\*(C`WHILE\*(C'\fR loops. .PP .Vb 4 \& [% FOR item IN data \-%] \& [% LAST IF item == 42 \-%] \& ... \& [% END \-%] .Ve .SS "Conditional statements" .IX Subsection "Conditional statements" .Vb 7 \& [% IF logical_expression %] \& Case 1 \& [% ELSIF logical_expression %] \& Case 2 \& [% ELSE %] \& Case 3 \& [% END %] \& \& [% UNLESS logical_expression %] \& Case 1 \& [% ELSE %] \& Case 2 \& [% END %] \& \& [% SWITCH expression %] \& [% CASE case1 %] \& Case 1 \& [% CASE case2 %] \& Case 2 \& [% CASE DEFAULT %] \& Case 3 \& [% END %] .Ve .SS "Functions and filters" .IX Subsection "Functions and filters" .Vb 2 \& [% var | f %] \& [% f(var) %] .Ve .SS "Template inclusion" .IX Subsection "Template inclusion" The \f(CW\*(C`INCLUDE\*(C'\fR statement is supported. .PP .Vb 2 \& [% INCLUDE "file.tt" %] \& [% INCLUDE $var %] .Ve .PP \&\f(CW\*(C`WITH variables\*(C'\fR syntax is also supported, although the \f(CW\*(C`WITH\*(C'\fR keyword is optional in Template-Toolkit: .PP .Vb 5 \& [% INCLUDE "file.tt" WITH foo = 42, bar = 3.14 %] \& [% INCLUDE "file.tt" WITH \& foo = 42 \& bar = 3.14 \& %] .Ve .PP The \f(CW\*(C`WRAPPER\*(C'\fR statement is also supported. The argument of \f(CW\*(C`WRAPPER\*(C'\fR, however, must be string literals, because templates will be statically linked while compiling. .PP .Vb 3 \& [% WRAPPER "file.tt" %] \& Hello, world! \& [% END %] \& \& %%# with variable \& [% WRAPPER "file.tt" WITH title = "Foo!" %] \& Hello, world! \& [% END %] .Ve .PP The content will be set into \f(CW\*(C`content\*(C'\fR, but you can specify its name with the \f(CW\*(C`INTO\*(C'\fR keyword. .PP .Vb 3 \& [% WRAPPER "foo.tt" INTO wrapped_content WITH title = "Foo!" %] \& ... \& [% END %] .Ve .PP This is a syntactic sugar to template cascading. Here is a counterpart of the example in Kolon. .PP .Vb 4 \& : macro my_content \-> { \& Hello, world! \& : } \& : cascade "file.tx" { content => my_content() } .Ve .PP Note that the WRAPPER option () in Template-Toolkit is not supported directly. Instead, you can emulate it with \f(CW\*(C`header\*(C'\fR and \f(CW\*(C`footer\*(C'\fR options as follows: .PP .Vb 3 \& my %vpath = ( \& wrap_begin => \*(Aq[% WRAPPER "base" %]\*(Aq, \& wrap_end => \*(Aq[% END %]\*(Aq, \& \& base => \*(AqHello, [% content %] world!\*(Aq . "\en", \& content => \*(AqXslate\*(Aq, \& ); \& \& my $tx = Text::Xslate\->new( \& syntax => \*(AqTTerse\*(Aq, \& path => \e%vpath, \& \& header => [\*(Aqwrap_begin\*(Aq], \& footer => [\*(Aqwrap_end\*(Aq], \& ); \& \& print $tx\->render(\*(Aqcontent\*(Aq); # => Hello, Xslate world!; .Ve .SS "Macro blocks" .IX Subsection "Macro blocks" Definition: .PP .Vb 3 \& [% MACRO foo BLOCK \-%] \& This is a macro. \& [% END \-%] \& \& [% MACRO add(a, b) BLOCK \-%] \& [% a + b \-%] \& [% END \-%] .Ve .PP Call: .PP .Vb 2 \& [% foo() %] \& [% add(1, 2) %] .Ve .PP Unlike Template-Toolkit, calling macros requires parens (\f(CW\*(C`()\*(C'\fR). .SS "Virtual methods" .IX Subsection "Virtual methods" A few methods are supported in the Xslate core. .PP .Vb 3 \& %% a.size(); \& %% a.join(", "); \& %% a.reverse(); \& \& %% h.size(); \& %% h.keys(); \& %% h.values(); \& %% h.kv(); .Ve .PP However, there is a bridge mechanism that allows you to use external methods. For example, Text::Xslate::Bridge::TT2 provides the TT2 virtual methods for Xslate, which bridges Template::VMethods implementation. .PP .Vb 1 \& use Text::Xslate::Bridge::TT2; \& \& my $tx = Text::Xslate\->new( \& syntax => \*(AqTTerse\*(Aq, \& module => [qw(Text::Xslate::Bridge::TT2)], \& ); \& \& print $tx\->render_string(\*(Aq[% "foo".length() %]\*(Aq); # => 3 .Ve .PP See Text::Xslate::Bridge, or search for \f(CW\*(C`Text::Xslate::Bridge::*\*(C'\fR on CPAN. .SS Misc. .IX Subsection "Misc." CALL evaluates expressions, but does not print it. .PP .Vb 1 \& [% CALL expr %] .Ve .PP SET and assignments, although the use of them are strongly discouraged. .PP .Vb 2 \& [% SET var1 = expr1, var2 = expr2 %] \& [% var = expr %] .Ve .PP DEFAULT statements as a syntactic sugar to \f(CW\*(C`SET var = var // expr\*(C'\fR: .PP .Vb 1 \& [% DEFAULT lang = "TTerse" %] .Ve .PP FILTER blocks to apply filters to text sections: .PP .Vb 3 \& [% FILTER html \-%] \& Hello, world! \& [% END \-%] .Ve .SH COMPATIBILITY .IX Header "COMPATIBILITY" There are some differences between TTerse and Template-Toolkit. .IP \(bu 4 \&\f(CW\*(C`INCLUDE\*(C'\fR of TTerse requires an expression for the file name, while that of Template-Toolkit allows a bare token: .Sp .Vb 2 \& [% INCLUDE foo.tt # doesn\*(Aqt work! %] \& [% INCLUDE "foo.tt" # OK %] .Ve .IP \(bu 4 \&\f(CW\*(C`FOREACH item = list\*(C'\fR is forbidden in TTerse. It must be \f(CW\*(C`FOREACH item IN list\*(C'\fR. .IP \(bu 4 TTerse does not support plugins (i.e. \f(CW\*(C`USE\*(C'\fR directive), but understands the \f(CW\*(C`USE\*(C'\fR keyword as an alias to \f(CW\*(C`CALL\*(C'\fR, so you could use some simple plugins which do not depend on the context object of Template-Toolkit. .Sp .Vb 2 \& use Template::Plugin::Math; \& use Template::Plugin::String; \& \& my $tt = Text::Xslate\->new(...); \& \& mt %vars = ( \& Math => Template::Plugin::Math\->new(), # as a namespace \& String => Template::Plugin::String\->new(), # as a prototype \& ); \& print $tt\->render_string(<<\*(AqT\*(Aq, \e%vars); \& [% USE Math # does nothing actually, only for compatibility %] \& [% USE String %] \& [% Math.abs(\-100) # => 100 %] \& [% String.new("foo").upper # => FOO %] .Ve .IP \(bu 4 The following directives are not supported: \&\f(CW\*(C`INSERT\*(C'\fR, \f(CW\*(C`PROCESS\*(C'\fR, \f(CW\*(C`BLOCK\*(C'\fR as a named blocks, \f(CW\*(C`USE\*(C'\fR (but see above), \&\f(CW\*(C`PERL\*(C'\fR, \f(CW\*(C`RAWPERL\*(C'\fR, \f(CW\*(C`TRY\*(C'\fR, \f(CW\*(C`THROW\*(C'\fR, \&\f(CW\*(C`RETURN\*(C'\fR, \f(CW\*(C`STOP\*(C'\fR, \f(CW\*(C`CLEAR\*(C'\fR, \f(CW\*(C`META\*(C'\fR, \f(CW\*(C`TAGS\*(C'\fR, \f(CW\*(C`DEBUG\*(C'\fR, and \f(CW\*(C`VIEW\*(C'\fR. .Sp Some might be supported in a future. .SH "SEE ALSO" .IX Header "SEE ALSO" Text::Xslate .PP Template (Template::Toolkit) .PP Template::Tiny .PP Text::Xslate::Bridge::TT2 .PP Text::Xslate::Bridge::TT2Like .PP Text::Xslate::Bridge::Alloy