DESCRIPTION

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

There are a lot of template engines in CPAN, for example Template-Toolkit, Text::MicroTemplate, HTML::Template, and so on, but all of them have demerits at some points. This is why Xslate is developed and now it is well-honed as the standard template engine for web applications.

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.

Other remarkable features are as follows:

Features

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.

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.

Smart escaping for HTML meta characters

All the HTML meta characters in template expressions the engine interpolates into template texts are escaped automatically, 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.

Easiness to enhance

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

INTERFACE

Methods

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, which is 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 for productions to avoid conflicts of template names.

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.

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.

Note that this is used for static name resolution. That is, the compiler uses it but the runtime engine doesn't.

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, smart escaping is applied to template expressions. That is, they are interpolated via the html_escape filter. If $type is text smart escaping is not applied so that it is suitable for plain texts like e-mails.

$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, the line code style is 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.

footer => \@template_files

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

This option is passed to the compiler.

warn_handler => \&cb

Specify the callback &cb which is called on warnings.

This option is experimental.

die_handler => \&cb

Specify the callback &cb which is called on fatal errors.

This option is experimental.

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

Renders a template file with given 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 given variables, and returns the result. \%vars is optional.

Note that $string is never cached, so this method should be avoided in production environment. If you want in-memory templates, consider the path option for HASH references which are cached as you expect: