Text::Xslate::Syntax::TTerse - An alternative syntax like Template-Toolkit 2
use Text::Xslate; my $tx = Text::Xslate->new( syntax => 'TTerse', ); print $tx->render_string( 'Hello, [% dialect %] world!', { dialect => 'TTerse' } );
TTerse is a subset of the Template-Toolkit 2.0 (and partially 3.0) syntax, using [% ... %] tags and %% ... line code.
[% ... %]
%% ...
This support Template-Toolkit like syntax, but the details might be different.
Note that lower-cased keywords, which are inspired in Template-Toolkit 3, are also allowed.
Scalar access:
[% var %] [% $var %]
Field access:
[% var.0 %] [% var.field %] [% var.accessor %] [% var.$field ]% [% var[$field] # TTerse specific %]
Variables may be HASH references, ARRAY references, or objects.
If $var is an object instance, you can call its methods.
[% $var.method() %] [% $var.method(1, 2, 3) %] [% $var.method(foo => [1, 2, 3]) %] [% $var.method({ foo => 'bar' }) %]
Almost the same as Text::Xslate::Syntax::Kolon, but the _ operator for concatenation is supported for compatibility.
_
[% FOREACH item IN arrayref %] * [% item %] [% END %]
Loop iterators are partially supported.
[% FOREACH item IN arrayref %] [%- IF loop.is_first -%] <first> [%- END -%] * [% loop.index %] * [% loop.count # loop.index + 1 %] * [% loop.body # alias to arrayref %] * [% loop.size # loop.body.size %] * [% loop.max # loop.size - 1 %] * [% loop.peek_next # loop.body[ loop.index - 1 ] * [% loop.peek_prev # loop.body[ loop.index + 1 ] [%- IF loop.is_last -%] <last> [%- END -%] [% END %]
For compatibility with Template-Toolkit, first for is_first, last for is_last, next for peek_next, prev for peek_prev are supported, but the use of them is discouraged because they are hard to understand.
first
is_first
last
is_last
next
peek_next
prev
peek_prev
[% IF expression %] This is true [% ELSE %] Tis is false [% END %] [% IF expression %] Case 1 [% ELSIF expression %] Case 2 [% ELSE %] Case 3 [% END %]
[% var | f %] [% f(var) %]
The INCLUDE statement is supported.
INCLUDE
[% INCLUDE "file.tt" %] [% INCLUDE $var %]
WITH variablies syntax is also supported, although the WITH keyword is optional in Template-Toolkit:
WITH variablies
WITH
[% INCLUDE "file.tt" WITH foo = 42, bar = 3.14 %] [% INCLUDE "file.tt" WITH foo = 42 bar = 3.14 %]
The WRAPPER statement is also supported. The argument of WRAPPER, however, must be string literals, because templates will be statically linked while compiling.
WRAPPER
[% WRAPPER "file.tt" %] Hello, world! [% END %] %%# with variable [% WRAPPER "file.tt" WITH title = "Foo!" %] Hello, world! [% END %]
The content will be set into content, but you can specify its name with the INTO keyword.
content
INTO
[% WRAPPER "foo.tt" INTO wrapped_content WITH title = "Foo!" %] ... [% END %]
This is a syntactic sugar to template cascading. Here is a counterpart of the example in Kolon.
: macro my_content -> { Hello, world! : } : cascade "file.tx" { content => my_content() }
Definition:
[% MACRO foo BLOCK -%] This is a macro. [% END -%] [% MACRO add(a, b) BLOCK -%] [% a + b -%] [% END -%]
Call:
[% foo() %] [% add(1, 2) %]
Unlike Template-Toolkit, calling macros requires parens (()).
()
A few methods are supported in the Xslate core.
%% any.defined() %% a.size(); %% a.join(", "); %% a.reverse(); %% h.size(); %% h.keys(); %% h.values(); %% h.kv();
However, there is a bridge mechanism that allows you to use more methods. For example, Text::Xslate::Bridge::TT2 provides the TT2 pseudo methods (a.k.a virtual methods) for Xslate, which uses Template::VMethods implementation.
use Text::Xslate::Bridge::TT2; my $tx = Text::Xslate->new( module => [qw(Text::Xslate:*Bridge::TT2)], ); print $tx->render_strig('[% "foo".length() %]'); # => 3
See Text::Xslate::Bridge, Text::Xslate::Bridge::TT2, and Text::Xslate::Bridge::Alloy for details.
CALL evaluates expressions, but does not print it.
[% CALL expr %]
SET and assignments are supported, although the use of them are strongly discouraged.
[% SET var1 = expr1, var2 = expr2 %] [% var = expr %]
Text::Xslate
Template::Toolkit
Template::Tiny
To install Text::Xslate, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Text::Xslate
CPAN shell
perl -MCPAN -e shell install Text::Xslate
For more information on module installation, please visit the detailed CPAN module installation guide.