++ed by:
KABLAMO GFUJI JEREMIAH RSIMOES TOMITA

32 PAUSE users
15 non-PAUSE users.

藤 吾郎
and 1 contributors

NAME

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

SYNOPSIS

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

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

DESCRIPTION

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

SYNTAX

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.

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       # 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.

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

Template cascading

Not supported.

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

SEE ALSO

Text::Xslate

Template::Toolkit

Template::Tiny