SlideShare a Scribd company logo
Xslate, a template engine
Goro Fuji
gfuji@cpan.org
2013-7-11 @ SVPerl
Myself
Call me Goro
Working at Sunnyvale from May 2013
CPAN author
Xslate, Mouse, patches to Perl itself
My favorites
Perl as a text processor
esp. regular expressions
Perl as a testing driver
JSX, a typed JavaScript
My Requests
Feel free to ask for questions
Please say it slowly and clearly XD
Agenda
What is a template engine
What is Xslate
How to use Xslate
What is a template
engine
Modules to build a text with dynamic
parameters
Without Template Engine
sprintf(“Hello, %s”, “world”)
“Hello %HOME%” =~ s/%(w+)%/$ENV{$1}/gr
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!
When to use?
Make HTML pages
Make mail reports
Whenever you build a text with parameters
CPAN Template Engines
Template Toolkit
Mason
HTML::Template (::Pro)
Mojo::Template
Text::Xslate
and more
What is Xslate
Text::Xslate
Heavily inspired in:
Template Toolkit
Text::MicroTemplate
Template Toolkit
or TT2
Super popular
A lot of features and plugins
Easy to learn
XSS vulnerability
Text::MicroTemplate
or TMT
A tiny template engine
Much faster than TT2
Written in pure Perl
Smart escaping (XSS guard)
Smart Escaping (1)
XSS: <a href=”blah”><: $foo :></a>
where $foo is <script>alert(“XSS”)</script>
What does the template engine do?
Smart Escaping
TT2: prints it as is
TMT: prints &lt;script&gt;alert(“XSS”)&lt;/script&gt;
escapes HTML meta characters (<, >, &, and etc.)
decides escaping by data type (described later)
means it is safer than writing HTML by yourself
Xslate
100+ times faster than TT2
Smart escaping, the same as TMT
Good for Plack/PSGI
Try Xslate
install: cpanm Text::Xslate
cli: xslate -e ‘Hello, <: $ARGV[0] :>’ Xslate
How to use Xslate
From Perl
use Text::Xslate;
my $tx = Text::Xslate->new();
print $tx->render($file, %vars);
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
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
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, ...
Include
include “foo.tx” # expand the template there
include “foo.tx” { foo => “bar” } # with vars
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
Utilities
need: Text::Xslate->new(module =>
[“Text::Xslate::Bridge::Star”])
and perldoc Text::Xslate::Manual::Builtin
substr(), uc(), lc(), sprintf(), etc, etc
<: function($arg) :> or <: $arg | function :>
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”
Conclusion
Xslate is a
super fast,
powerful,
and XSS-free template engine

More Related Content

PDF
DBIx::Class beginners
ODP
HTML Templates Using Clear Silver
PDF
DBIx::Class introduction - 2010
ODP
KEY
Apache Velocity 1.6
PPT
LPW: Beginners Perl
PPTX
Quick beginner to Lower-Advanced guide/tutorial in PHP
PDF
PHP Arrays - indexed and associative array.
DBIx::Class beginners
HTML Templates Using Clear Silver
DBIx::Class introduction - 2010
Apache Velocity 1.6
LPW: Beginners Perl
Quick beginner to Lower-Advanced guide/tutorial in PHP
PHP Arrays - indexed and associative array.

What's hot (20)

PDF
Data Types In PHP
PPT
PHP variables
KEY
Introduction to Perl Best Practices
PPT
Introducation to php for beginners
PPT
Intro to php
PPTX
Phphacku iitd
PPTX
PHP for hacks
PDF
WordPress London 16 May 2012 - You don’t know query
PPT
Php mysql
PPT
Intro to PHP
ODP
Extending Zend Framework
PDF
Php Tutorials for Beginners
PPTX
You don’t know query - WordCamp UK Edinburgh 2012
PPTX
Php server variables
PPT
PHP POWERPOINT SLIDES
PPT
Php mysql
PDF
Sorting arrays in PHP
PPT
PHP - Introduction to PHP
Data Types In PHP
PHP variables
Introduction to Perl Best Practices
Introducation to php for beginners
Intro to php
Phphacku iitd
PHP for hacks
WordPress London 16 May 2012 - You don’t know query
Php mysql
Intro to PHP
Extending Zend Framework
Php Tutorials for Beginners
You don’t know query - WordCamp UK Edinburgh 2012
Php server variables
PHP POWERPOINT SLIDES
Php mysql
Sorting arrays in PHP
PHP - Introduction to PHP
Ad

Similar to Xslate sv perl-2013-7-11 (20)

ODP
Let's use Xslate - Osdc.tw 2011
ODP
How Xslate Works
PDF
Hiveminder - Everything but the Secret Sauce
PPT
Learning XSLT
PDF
The Lumber Mill Xslt For Your Templates
PDF
Diving deep into twig
ODP
Introduction to Modern Perl
ODP
Template Toolkit
PDF
Beijing Perl Workshop 2008 Hiveminder Secret Sauce
PPT
PPTX
programming language interface i.pptx
PDF
The Lumber Mill - XSLT For Your Templates
PPS
Web Development in Perl
PDF
Efficient Context-sensitive Output Escaping for Javascript Template Engines
PDF
Perl 101
PDF
What we can learn from Rebol?
PPTX
PPTX
Unit 1-introduction to perl
PDF
Introduction To Perl - SpringPeople
Let's use Xslate - Osdc.tw 2011
How Xslate Works
Hiveminder - Everything but the Secret Sauce
Learning XSLT
The Lumber Mill Xslt For Your Templates
Diving deep into twig
Introduction to Modern Perl
Template Toolkit
Beijing Perl Workshop 2008 Hiveminder Secret Sauce
programming language interface i.pptx
The Lumber Mill - XSLT For Your Templates
Web Development in Perl
Efficient Context-sensitive Output Escaping for Javascript Template Engines
Perl 101
What we can learn from Rebol?
Unit 1-introduction to perl
Introduction To Perl - SpringPeople
Ad

Recently uploaded (20)

PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Hybrid model detection and classification of lung cancer
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
DP Operators-handbook-extract for the Mautical Institute
PDF
Getting Started with Data Integration: FME Form 101
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
1. Introduction to Computer Programming.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Encapsulation theory and applications.pdf
PDF
Enhancing emotion recognition model for a student engagement use case through...
PPTX
A Presentation on Touch Screen Technology
PPTX
Tartificialntelligence_presentation.pptx
PDF
Approach and Philosophy of On baking technology
PDF
Web App vs Mobile App What Should You Build First.pdf
PPTX
Chapter 5: Probability Theory and Statistics
PDF
1 - Historical Antecedents, Social Consideration.pdf
PPTX
TLE Review Electricity (Electricity).pptx
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Encapsulation_ Review paper, used for researhc scholars
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Hybrid model detection and classification of lung cancer
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
DP Operators-handbook-extract for the Mautical Institute
Getting Started with Data Integration: FME Form 101
Digital-Transformation-Roadmap-for-Companies.pptx
1. Introduction to Computer Programming.pptx
Programs and apps: productivity, graphics, security and other tools
Encapsulation theory and applications.pdf
Enhancing emotion recognition model for a student engagement use case through...
A Presentation on Touch Screen Technology
Tartificialntelligence_presentation.pptx
Approach and Philosophy of On baking technology
Web App vs Mobile App What Should You Build First.pdf
Chapter 5: Probability Theory and Statistics
1 - Historical Antecedents, Social Consideration.pdf
TLE Review Electricity (Electricity).pptx
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Encapsulation_ Review paper, used for researhc scholars

Xslate sv perl-2013-7-11

Editor's Notes

  • #13: Because Xslate has been inspired in TT2 Text::MicroTemplate, I’d like to introduce some features about them
  • #14: TT2 is super popular template
  • #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”.