++ed by:

34 PAUSE users
26 non-PAUSE users.

藤 吾郎
and 1 contributors


Text::Xslate - High performance template engine


This document describes Text::Xslate version 0.1020.


    use Text::Xslate;
    use FindBin qw($Bin);

    my $tx = Text::Xslate->new(
        # the fillowing options are optional.
        path       => ['.'],
        cache_dir  => File::Spec->tmpdir,
        cache      => 1,

    my %vars = (
        title => 'A list of books',
        books => [
            { title => 'Islands in the stream' },
            { title => 'Programming Perl'      },
            { title => 'River out of Eden'     },
            { title => 'Beautiful code'        },

    # for files
    print $tx->render('hello.tx', \%vars);

    # for strings
    my $template = q{
        <h1><: $title :></h1>
        : for $books ->($book) {
            <li><: $book.title :></li>
        : } # for

    print $tx->render_string($template, \%vars);

    # you can tell the engine that some strings are already escaped.
    use Text::Xslate qw(escaped_string);

    $vars{email} = escaped_string('gfx &lt;gfuji at cpan.org&gt;');
    # or
    $vars{email} = Text::Xslate::EscapedString->new(
        'gfx &lt;gfuji at cpan.org&gt;',
    ); # if you don't want to pollute your namespace.

    # if you want Template-Toolkit syntx:
    $tx = Text::Xslate->new(syntax => 'TTerse');
    # ...


Text::Xslate is a template engine tuned for persistent applications. This engine introduces the virtual machine paradigm. That is, templates are compiled into xslate intermediate code, and then executed by the xslate virtual machine.

The philosophy for Xslate is sandboxing that the template logic should not have no access outside the template beyond your permission.

This software is under development. Version 0.1xxx is a developing stage, which may include radical changes. Version 0.2xxx and more will be somewhat stable.


High performance

Xslate has a virtual machine written in XS, which is highly optimized. According to benchmarks, Xslate is 2-10 times faster than other template engines (Template-Toolkit, HTML::Template::Pro, Text::MicroTemplate, etc.).

Template cascading

Xslate supports template cascading, which allows you to extend templates with block modifiers. It is like traditional template inclusion, but is more powerful.

This mechanism is also called as template inheritance.

Syntax alternation

The Xslate virtual machine and the parser/compiler are completely separated so that one can use alternative parsers.

For example, TTerse, a Template-Toolkit-like parser, is supported as a completely different syntax.



Text::Xslate->new(%options) :XslateEngine

Creates a new xslate template engine.

Possible options are:

path => \@path // ['.']

Specifies the include paths.

cache => $level // 1

Sets the cache level.

If $level == 1 (default), Xslate caches compiled templates on the disk, and checks the freshness of the original templates every time.

If $level >= 2, caches will be created but the freshness will not be checked.

$level == 0 creates no caches. It's provided for testing.

cache_dir => $dir // File::Spec->tmpdir

Specifies the directory used for caches.

function => \%functions

Specifies functions, which may be called as f($arg) or $arg | f.

module => [$module => ?\@import_args, ...]

Imports functions from $module. @import_args is optional.

For example:

    my $tx = Text::Xslate->new(
        module => ['Data::Dumper'], # use Data::Dumper
    print $tx->render_string(
        '<: Dumper($x) :>',
        { x => [42] },
    # => $VAR = [42]

You can use function based modules with the module option, and also can invoke object methods in templates. Thus, Xslate doesn't require the namespaces for plugins.

input_layer => $perliolayers // ':utf8'

Specifies PerlIO layers for reading templates.

syntax => $name // 'Kolon'

Specifies the template syntax you want to use.

$name may be a short name (moniker), or a fully qualified name.

escape => $mode // 'html'

Specifies the escape mode, which is automatically applied to template expressions.

Possible escape modes are html and none.

verbose => $level // 1

Specifies the verbose level.

If $level == 0, all the possible errors will be ignored.

If $level> >= 1 (default), trivial errors (e.g. to print nil) will be ignored, but severe errors (e.g. for a method to throw the error) will be warned.

If $level >= 2, all the possible errors will be warned.

$tx->render($file, \%vars) :Str

Renders a template file with variables, and returns the result. \%vars can be omitted.

Note that $file may be cached according to the cache level.

$tx->render_string($string, \%vars) :Str

Renders a template string with variables, and returns the result. \%vars can be omitted.

Note that $string is never cached so that this method is suitable for testing.

$tx->load_file($file) :Void

Loads $file for following render($file, \%vars). Compiles and saves it as caches if needed.

This method can be used for pre-compiling template files.

Exportable functions

escaped_string($str :Str) -> EscapedString

Marks $str as escaped. Escaped strings will not be escaped by the template engine, so you have to escape these strings by yourself.

For example:

    my $tx   = Text::Xslate->new();
    my $tmpl = 'Mailaddress: <: $email :>';
    my %vars = (
        email => 'Foo &lt;foo@example.com&gt;',
    print $tx->render_string($tmpl, \%email);
    # => Mailaddress: Foo &lt;foo@example.com&gt;

html_escape($str :Str) -> EscapedString

Escapes html special characters in $str, and returns an escaped string (see above).


There are several syntaxes you can use:


Kolon is the default syntax, using <: ... :> tags and : ... line code, which is explained in Text::Xslate::Syntax::Kolon.


Metakolon is the same as Kolon except for using [% ... %] tags and % ... line code, instead of <: ... :> and : ....


TTerse is a syntax that is a subset of Template-Toolkit 2, which is explained in Text::Xslate::Syntax::TTerse.


There are common notes in the Xslate virtual machine.

Nil handling

Note that nil handling is different from Perl's.

to print

Prints nothing.

to access fields.

Returns nil. That is, nil.foo.bar.baz produces nil.

to invoke methods

Returns nil. That is, nil.foo().bar().baz() produces nil.

to iterate

Dealt as an empty array.


$var == nil returns true if and only if $var is nil.


Perl 5.10.0 or later.


All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT. Patches are welcome :)


Xslate template syntaxes:




Xslate command:


Other template modules:












Thanks to lestrrat for the suggestion to the interface of render() and the contribution of App::Xslate.

Thanks to tokuhirom for the ideas, feature requests, encouragement, and bug-finding.

Thanks to gardejo for the proposal to the name template cascading.

Thanks to jjn1056 to the concept of template overlay (now implemented as cascade with ...).

Thanks to makamaka for the contribution of Text::Xslate::PP.


Fuji, Goro (gfx) <gfuji(at)cpan.org>

Makamaka Hannyaharamitu

Maki, Daisuke (lestrrat)


Copyright (c) 2010, Fuji, Goro (gfx). All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.