++ed by:
31 non-PAUSE users
Author image FIUJI, Goro
and 1 contributors


Text::Xslate - Scalable template engine for Perl5


This document describes Text::Xslate version 0.3000.


    use Text::Xslate;

    my $tx = Text::Xslate->new(
        # the fillowing options are optional.
        path       => ['.'],
        cache_dir  => "$ENV{HOME}/.xslate_cache",
        cache      => 1,

    my %vars = (
        title => 'A list of books',
        books => [
            { title => 'Islands in the stream' },
            { title => 'Programming Perl'      },
            # ...

    # 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);


Text::Xslate is a template engine, tuned for persistent applications, safe as an HTML generator, and with rich features.

The concept of Xslate is strongly influenced by Text::MicroTemplate and Template-Toolkit 2, but the central philosophy of Xslate is different from them. That is, the philosophy is sandboxing that the template logic should not have no access outside the template beyond your permission.


High performance

This engine introduces the virtual machine paradigm. Templates are compiled into intermediate code, and then executed by the virtual machine, which is highly optimized for rendering templates. Thus, Xslate is much faster than any other template engines.

Here is a result of benchmark/x-rich-env.pl to compare various template engines in "rich" environment where applications are persistent and XS modules are available.

    $ perl -Mblib benchmark/x-rich-env.pl
    Perl/5.10.1 i686-linux
    ok 1 - TT: Template-Toolkit
    ok 2 - MT: Text::MicroTemplate
    ok 3 - TCS: Text::ClearSilver
    ok 4 - HTP: HTML::Template::Pro
    Benchmarks with 'include' (datasize=100)
              Rate     TT     MT    TCS    HTP Xslate
    TT       129/s     --   -84%   -94%   -95%   -99%
    MT       807/s   527%     --   -63%   -71%   -96%
    TCS     2162/s  1580%   168%     --   -23%   -89%
    HTP     2814/s  2087%   249%    30%     --   -85%
    Xslate 19321/s 14912%  2295%   794%   587%     --

According to this result, Xslate is 100+ times faster than Template-Toolkit. Text::MicroTemplate is a very fast template engine written in pure Perl, but XS-based modules, namely Text::ClearSilver, HTML::Template::Pro and Xslate are faster than Text::MicroTemplate. Moreover, Xslate is even faster than Text::ClearSilver and HTML::Template::Pro.

There are benchmark scripts in the benchmark/ directory.

Auto escaping to HTML meta characters

All the HTML meta characters in template expressions the engine interpolates into template texts are automatically escaped, so the output has no possibility to XSS by default.

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.

Easy to enhance

Xslate is highly extensible. You can add functions and methods to the template engine and even add a new syntax via extending the parser.



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

Creates a new xslate template engine with options. You can reuse the instance for multiple call of render().

Possible options are:

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

Specifies the include paths, which may be directory names or virtual paths, i.e. HASH references which contain $file_name => $content pairs.

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 uses no caches. It's provided for testing.

cache_dir => $dir // "$ENV{HOME}/.xslate_cache"

Specifies the directory used for caches. If $ENV{HOME} doesn't exist, File::Spec->tmpdir will be used.

You should specify this option on productions.

function => \%functions

Specifies a function map which contains name-coderef pairs. A function f may be called as f($arg) or $arg | f in templates.

There are builtin filters which are not overridable.

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

Imports functions from $module, which may be a function-based or bridge module. Optional @import_args are passed to import as $module->import(@import_args).

For example:

    # for function-based modules
    my $tx = Text::Xslate->new(
        module => ['Time::Piece'],
    print $tx->render_string(
        '<: localtime($x).strftime() :>',
        { x => time() },
    ); # => Wed, 09 Jun 2010 10:22:06 JST

    # for bridge modules
    my $tx = Text::Xslate->new(
        module => ['SomeModule::Bridge::Xslate'],
    print $tx->render_string(
        '<: $x.some_method() :>',
        { x => time() },

Because you can use function-based modules with the module option, and also can invoke any object methods in templates, Xslate doesn't require specific namespaces for plugins.

input_layer => $perliolayers // ':utf8'

Specifies PerlIO layers to open template files.

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.

suffix => $ext // '.tx'

Specify the template suffix, which is used for cascade and include in Kolon.

syntax => $name // 'Kolon'

Specifies the template syntax you want to use.

$name may be a short name (e.g. Kolon), or a fully qualified name (e.g. Text::Xslate::Syntax::Kolon).

This option is passed to the compiler directly.

type => $type // 'html'

Specifies the output content type. If $type is html or xml, template expressions are interpolated via the html-escape filter. If $type is text, template expressions are interpolated as they are.

$type may be html, xml (identical to html), and text.

This option is passed to the compiler directly.

line_start => $token // $parser_defined_str

Specify the token to start line code as a string, which quotemeta will be applied to. If you give undef to this option, the line code style will be disabled.

This option is passed to the parser via the compiler.

tag_start => $str // $parser_defined_str

Specify the token to start inline code as a string, which quotemeta will be applied to.

This option is passed to the parser via the compiler.

tag_end => $str // $parser_defined_str

Specify the token to end inline code as a string, which quotemeta will be applied to.

This option is passed to the parser via the compiler.

header => \@template_files

Specify the header template files, which are inserted to the head of each template.

This option is passed to the compiler.

Specify the footer template files, which are inserted to the foot of each template.

This option is passed to the compiler.

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

Renders a template file with variables, and returns the result. \%vars is optional.

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 is optional.

Note that $string is never cached, so this method may not be suitable for productions.

$tx->load_file($file) :Void

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

It is a good idea to load templates before applications fork. Here is an example to to load all the templates which is in a given path:

    my $path = ...;
    my $tx = Text::Xslate->new(
        path      => [$path],
        cache_dir =>  $path,

    find sub {
        if(/\.tx$/) {
            my $file = $File::Find::name;
            $file =~ s/\Q$path\E .//xsm; # fix path names
    }, $path;

    # fork and render ...

Text::Xslate->current_engine :XslateEngine

Returns the current Xslate engine while executing. Otherwise returns undef. This method is significant when it is called by template functions and methods.

Text::Xslate->current_file :Str

Returns the current file name while executing. Otherwise returns undef. This method is significant when it is called by template functions and methods.

Text::Xslate->current_line :Int

Returns the current line number while executing. Otherwise returns undef. This method is significant when it is called by template functions and methods.

Exportable functions

mark_raw($str :Str) :RawStr

Marks $str as raw, so that the content of $str will be rendered as is, so you have to escape these strings by yourself.

For example:

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

This function is available in templates as the mark_raw filter, although the use of it is discouraged.

unmark_raw($str :Str) :Str

Clears the raw marker from $str, so that the content of $str will be escaped before rendered.

This function is available in templates as the unmark_raw filter.

html_escape($str :Str) :RawStr

Escapes HTML meta characters in $str, and returns it as a raw string (see above). If $str is already a raw string, it returns $str as is.

By default, this function will be automatically applied to all the template expressions.

This function is available in templates as the html filter, but you'd better to use unmark_raw to ensure expressions to be html-escaped.

uri_escape($str :Str) :Str

Escapes URI unsafe characters in $str, and returns it.

This function is available in templates as the uri filter.

html_builder { block } | \&function :CodeRef

Wraps &function with mark_raw so that the new subroutine returns a raw string.

This function is used to tell the xslate engine that &function is an HTML builder that returns HTML sources. For example:

    sub some_html_builder {
        my $html;
        # build HTML ...
        return $html;

    my $tx = Text::Xslate->new(
        function => {
            some_html_builder => html_builder(\&some_html_builder),

See also Text::Xslate::Manual::Cookbook.

Command line interface

The xslate(1) command is provided as a CLI to the Text::Xslate module, which is used to process directory trees or to evaluate one liners. For example:

    $ xslate -D name=value -o dest_path src_path

    $ xslate -e 'Hello, <: $ARGV[0] :> wolrd!' Xslate
    $ xslate -s TTerse -e 'Hello, [% ARGV.0 %] world!' TTerse

See xslate(1) for details.


There are multiple template syntaxes available in Xslate.


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


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


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


There are common notes in Xslate.

Nil/undef handling

Note that nil (i.e. undef in Perl) handling is different from Perl's. Basically it does nothing, but verbose => 2 will produce warnings on it.

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.8.1 or later.

If you have a C compiler, the XS backend will be used. Otherwise the pure Perl backend will be used.


  • Context controls. e.g. <: [ $foo->bar @list ] :>.

  • Augment modifiers.

  • Loop controls.

  • Default arguments and named arguments for macros.


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:


The Xslate web site and public repository:



Other template modules that Xslate has been influenced by:




Template (Template::Toolkit)









Thanks to lestrrat for the suggestion to the interface of render(), the contribution of Text::Xslate::Runner (was App::Xslate), and a lot of suggestions.

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.

Thanks to typester for the various inspirations.

Thanks to clouder for the patch of adding AND and OR to TTerse.

Thanks to punytan for the documentation improvement.

Thanks to chiba for the bug reports and patches.

Thanks to turugina for the patch to fix Win32 problems

Thanks to Sam Graham to the bug reports.

Thanks to Mons Anderson to the bug reports and patches.


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

Makamaka Hannyaharamitu (makamaka) (Text::Xslate::PP)

Maki, Daisuke (lestrrat) (Text::Xslate::Runner)


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.