Text::Xslate - High performance template engine (ALPHA)
This document describes Text::Xslate version 0.001_03.
use Text::Xslate; use FindBin qw($Bin); 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 multiple files my $tx = Text::Xslate->new(file => [qw(hello.tx)]); print $tx->render_file('hello.tx', \%vars); # for strings my $template = q{ <h1><:= $title :></h1> <ul> : for $books ->($book) { <li><:= $book.title :></li> : } # for </ul> }; $tx = Text::Xslate->new( string => $template, ); print $tx->render(\%vars);
Text::Xslate is a template engine tuned for persistent applications. This engine introduces virtual machines. That is, templates are compiled into xslate opcodes, and then executed by the xslate virtual machine just like as Perl does.
This software is under development. Any interfaces will be changed.
Creates a new xslate template code.
Options:
string => $template_string
file => $template_file | \@template_files
path => \@path // ["$FindBin::Bin/../template"]
function => \%functions
auto_compile => $bool // true
Renders a template with variables, and returns the result.
TODO
<:= $var :> <:= $var.field :> <:= $var["field"] :>
Variables may be HASH references, ARRAY references, or objects.
for
: for $data ->($item) { [<:= $item.field =>] : }
Iterating data may be ARRAY references.
if
: if $var == nil { $var is nil. : } : else if $var != "foo" { $var is not nil nor "foo". : } : else { $var is "foo". : } : if( $var >= 1 && $var <= 10 ) { $var is 1 .. 10 : } := $var.value == nil ? "nil" : $var.value
Relational operators (== != < <= > >=):
== != < <= > >=
:= $var == 10 ? "10" : "not 10" := $var != 10 ? "not 10" : "10"
Arithmetic operators (+ - * / %):
+ - * / %
:= $var + 10 := ($var % 10) == 0
Logical operators (|| && //)
|| && //
:= $var >= 0 && $var <= 10 ? "ok" : "too smaller or too larger" := $var // "foo" # as a default value
Operator precedence:
(TODO)
(NOT YET IMPLEMENTED)
Base templates mytmpl/base.tx:
: block title -> { # with default [My Template!] : } : block body is abstract # without default
Derived templates mytmpl/foo.tx:
: extends base : # use default title : override body { My Template Body! : }
Derived templates mytmpl/bar.tx:
: extends foo : # use default title : before body { Before body! : } : after body { After body! : }
Then, Perl code:
my $tx = Text::Xslate->new( file => 'mytmpl/bar.tx' ); $tx->render({});
Output:
[My Template!] Before body! My Template Body! Before Body!
Documentation
Template inheritance (like Text::MicroTemplate::Extended)
Opcode-to-XS compiler
Perl 5.10.0 or later, and a C compiler.
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.
Text::MicroTemplate
Text::ClearSilver
Template
HTML::Template
HTML::Template::Pro
Goro Fuji (gfx) <gfuji(at)cpan.org>
Copyright (c) 2010, Goro Fuji (gfx). All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic for details.
To install Text::Xslate, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Text::Xslate
CPAN shell
perl -MCPAN -e shell install Text::Xslate
For more information on module installation, please visit the detailed CPAN module installation guide.