Text::Xslate::Manual::FAQ - Frequently asked questions and answers
We read it /eks-leit/.
/eks-leit/
It stands for XS template, a template engine written in XS, although pure Perl implementations are also provided.
Xslate supports several template syntaxes. Kolon is the default syntax, Metakolon is suitable to output Kolon templates, and TTerse is compatible with Template-Toolkit. You can specify the template syntax by passing syntax option to the Text::Xslate constructor.
syntax
my $tx = Text::Xslate->new( syntax => 'TTerse', # by moniker ); my $tx = Text::Xslate->new( syntax => 'Text::Xslate::Syntax::TTerse', # by fully qualified name );
Use start_tag, end_tag, and line_start options, which can be joined together with syntax option:
start_tag
end_tag
line_start
my $tx = Text::Xslate->new( syntax => 'TTerse', tag_start => '{', tag_end => '}', line_start => undef, ); print $tx->render_string('Hello, {lang} world!', { lang => 'Xslate' });
Convert HASH references into ARRAY references because for methods can deal with ARRAY references.
for
: # in Kolon : # iterate $hash by keys : for $hash.keys() -> $key { <: $key :> : } : # by values : for $hash.values() -> $value { <: $value :> : } : # by key-value pairs : for $hash.kv() -> $pair { <: $pair.key :>=<: $pair.value :> : }
Note that the above methods return ARRAY references sorted by the keys.
Xslate itself does not support these methods and filters, but there are modules on CPAN that implement them.
Text::Xslate::Bridge::TT2 provides almost all the TT methods and filters, but it requires Template-Toolkit itself.
Text::Xslate::Bridge::TT2Like provides the same features as Bridge::TT2, but it does not require the Template-Toolkit distribution.
Bridge::TT2
These bridge modules are useful not only for TTerse users, but also for Kolon users.
Macros are objects that overload &{}, the CODE dereference operator, so all you have to do is to call them, but don't check their types because they are not a real CODE reference.
&{}
my $tx = Text::Xslate->new( function => { 'count' => sub { my($a, $cb) = @_; # Don't check the type of $cb! return scalar grep { $cb->($_) } @{$a}; }, }, ); print $tx->render_string('<: count($a, -> $x { $x >= 50 }) :>', { a => [ 0 .. 100 ] }, ); # => 50
Text::Xslate
Text::Xslate::Manual
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.