Goro Fuji presented on the Xslate template engine. Xslate is heavily inspired by Template Toolkit and Text::MicroTemplate. It aims to be 100+ times faster than Template Toolkit with smart escaping of HTML to prevent XSS vulnerabilities, similar to Text::MicroTemplate. The presentation covered how to install and use Xslate from Perl, supported features like variables, loops, includes, and utilities.
8. With Template Egine
use Text::Xslate;
my $xslate = Text::Xslate->new();
say $xslate->render(hello.tx, { a => Xslate);
# where hello.tx contains:
Hello, <: $a :> world!
9. When to use?
Make HTML pages
Make mail reports
Whenever you build a text with parameters
15. Smart Escaping (1)
XSS: <a href=blah><: $foo :></a>
where $foo is <script>alert(XSS)</script>
What does the template engine do?
16. Smart Escaping
TT2: prints it as is
TMT: prints <script>alert(XSS)</script>
escapes HTML meta characters (<, >, &, and etc.)
decides escaping by data type (described later)
means it is safer than writing HTML by yourself
21. Variables
<: $foo :> # where $foo is a scalar
<: $foo[0] :> # where $foo is an array ref
<: $foo[bar] :> # where $foo is an hash ref
<: $foo.bar(42) :> # where $foo is an object
22. if, else
<: if $foo { $bar } :>
# shows $bar if $foo looks like true
<: if $foo { :>plain text<: } :>
# separated blocks
<: if $a { } else if $b { } else { } :>
# not elsif
23. Loops and Special Vars
for $array_ref -> $item { ... } # foreach
for $a -> $item { $~item.count } # specials
$~item.count # 1, 2, 3, ...
$~item.index # 0, 1, 2, ...
$~item.cycle(a, b) # a, b, a, b, ...
25. Template Cascading
a.k.a. template inheritance
more powerful include
Like class inheritance
define a default behavior of components
override them in a sub template
27. From Perl
All the values are automatically escaped
but you can prevent them from escaping:
$vars{foo} = mark_raw($widget)
# where $widget includes HTML tags
# marks it to show it as is
#18: TT2 and TMT lead Xslate, which is extremely faster than TT2 and borrows smart escaping from TMT, and Xslate has been made after PSGI, its API is suitable for PSGI, BTW, do you know PSGI or Plack? PSGI is a web application specification just like as CGI and Plack is a toolkit compatible with PSGI. To be simple, a feature which runs web applications
#19: Do you know cpanm? This is a kind of cpan command but more fast and easy. And Xslate has a command line interface so you can easily evaluate a simple statement [DEMO]
#21: Use of Xslate is super simple. Just three statements. Loading, creating an instance, and rendering a template.
#25: When you want to split the template files, for example, to header files, body files, and footer files, you can use include directives.
#26: Template cascading, also known as template inheritance which is implemented in Django and Smarty, is another form of include.