NAME

Text::Xslate::Syntax::TTerse - An alternative syntax compatible with Template Toolkit 2

SYNOPSIS

use Text::Xslate;
my $tx = Text::Xslate->new(
    syntax => 'TTerse',
);

print $tx->render_string(
    'Hello, [% dialect %] world!',
    { dialect => 'TTerse' }
);

# PRE_PROCESS/POST_PROCESS/WRAPPER
$tx = Text::Xslate->new(
    syntax => 'TTerse',
);

DESCRIPTION

TTerse is a subset of the Template-Toolkit 2 (and partially 3) syntax, using [% ... %] tags and %% ... line code.

(TODO: I should concentrate on the difference between Template-Toolkit 2 and TTerse)

SYNTAX

This supports a Template-Toolkit compatible syntax, although the details might be different.

Note that lower-cased keywords, which are inspired in Template-Toolkit 3, are also allowed.

Variable access

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' }) %]

Expressions

Almost the same as Text::Xslate::Syntax::Kolon, but the _ operator for concatenation is supported for compatibility.

Loops

[% 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_index # 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 %]

Template-Toolkit compatible names are also supported, but the use of them is discouraged because they are not easy to understand:

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

Conditional statements

[% IF expression %]
    This is true
[% ELSE %]
    Tis is false
[% END %]

[% IF expression %]
    Case 1
[% ELSIF expression %]
    Case 2
[% ELSE %]
    Case 3
[% END %]

Functions and filters

[% var | f %]
[% f(var)  %]

Template inclusion

The INCLUDE statement is supported.

[% INCLUDE "file.tt" %]
[% INCLUDE $var %]

WITH variablies syntax is also supported, although the WITH keyword is optional in Template-Toolkit:

[% 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 "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.

[% 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() }

Macro blocks

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 (()).

Virtual methods

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.

Misc.

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 %]

DEFAULT statements are supported as a syntactic sugar to SET var = var // expr:

[% DEFAULT lang = "TTerse" %]

FILTER blocks are supported:

[% FILTER html -%]
Hello, <Xslate> world!
[% END -%]

SEE ALSO

Text::Xslate

Template::Toolkit

Template::Tiny