D-7 <altijd in beweging>

Day to day life of a Perl/Go/C/C++/whatever hacker. May include anything from tech, food, and family.

タグ:xslate

http://xslate.org/

tokuhiromがざーっと雛形作って、あとはどかどかみんなでコミットしてたらいつのまにかできてたよ。
    このエントリーをはてなブックマークに追加 mixiチェック Share on Tumblr

Text::Xslate is one of the latest inventions by Goro Fuji. It's yet another template engine -- and since it is yet another template engine, I could easily see everybody going "oh, *another* one....". Yeah, I get the idea. I feel the same for most new template engines.

But I have to say, this time... this one /is/ worth looking at.

First off, it's blazing fast. For trivial tasks, Text::Xslate (ex-slate) is 10 ~ 20 times faster than Template::Toolkit. Check out some of the benchmark scripts in the benchmark directory.

For example, here's demo-tt.pl ran on my Mac OS X:

daisuke@beefcake p5-Text-Xslate$ perl benchmark/demo-tt.pl 
Perl/5.12.1 darwin-2level
Text::Xslate/0.1030
Template/2.22
Template-Toolkit's process() x 1000
1000
Used: 3.457 sec.
Text::Xslate's render() x 1000
1000
Used: 0.207 sec.
In this benchmark, Xslate is about 16.7 times faster than Template-Tookit.

Now usually that kind of speed comes with a significant hit on the template language's usability. 

For example, templates like Mason or Text::MicroTemplate allows you to write raw Perl -- which /is/ good in some situations, but in my line of work, you need a moderately easy language for designers to work on. You can't quite expect designers to be able to decipher Perl code in the templates.

The really cool thing about Text::Xslate is that now it supports a 80 ~ 90% Template-Toolkit compatible template language, with all the variable methods and what not. I just switched my Template::Toolkit based Catalyst app to Text::Xslate with only minor changes in the template -- it was an easy task. And now the template rendering is blazing fast!

To use Text::Xslate in a sort-of-TT-compatible mode, do the following:
use strict;
use Text::Xslate;

my $xslate = Text::Xslate->new(
    syntax => "TTerse",
    module => [
        'Text::Xslate::Bridge::TT2Like',
        # or 'Text::Xslate::Bridge::TT2'
    ]
);
print $xslate->render_string( <<EOT, { foo=> "bar" } );
The value of foo is '[% foo %]'
Its length is [% foo.length() %]
If I perform s/foo/bar/, it becomes [% foo.replace('foo', 'bar') %]
EOT

The use of "TTerse" enables the use of TT compatible template tokens ('[%', '%]'), and enables TT like syntax like WRAPPER, INCLUDE, FOREACH, WHILE, SET, etc. DEFAULT and PROCESS are not supported, but from what I can tell you can live with it.

If you suspect you might have some performance bottleneck in your template rendering, I suggest you seriously consider this template engine. If you are a Catalyst-head like me, you can use my Catalyst::View::Xslate to easily integrate Text::Xslate and Catalyst.


Happy Xslate-ing!
    このエントリーをはてなブックマークに追加 mixiチェック Share on Tumblr

このページのトップヘ