See also L for details.
=head2 How to use Template Toolkit's WRAPPER feature in Kolon
Use template cascading, which is a super-set of the C directive.
F:

block content -> { }

F
: cascade wrapper
: override content -> {
Hello, world!
: }
Output:

Hello, world!

=head3 Template cascading
Xslate supports B, which allows you to extend
templates with block modifiers. It is like traditional template inclusion,
but is more powerful.
This mechanism is also called as template inheritance.
See also L.
=head2 How to map __DATA__ sections to the include path
Use C, and the C option of C, which accepts
HASH references which contain C<< $file_name => $content >> mapping.
use Text::Xslate;
use Data::Section::Simple;
my $vpath = Data::Section::Simple->new()->get_data_section();
my $tx = Text::Xslate->new(
path => [$vpath],
);
print $tx->render('child.tx');
__DATA__
@@ base.tx
{ :>default body
@@ child.tx
: cascade base;
: override body -> {
child body
: } # endblock body
This feature is directly inspired by Text::MicroTemplate::DataSection,
and originated from Mojolicious.
See also L, L,
and L.
=head2 How to interpolate data into JavaScript sections without XSS
Because Xslate escapes only HTML meta characters, you must escape
JavaScript meta characters by yourself when you put data into
C<< >> sections.
C module is not suitable because it doesn't escape some meta
characters such as C<< "" >>.
It is better to use utilities proven to be secure for JavaScript escaping
to avoid XSS.
L helps you for this aim.
my $tx = Text::Xslate->new(
module => ['JavaScript::Value::Escape' => [qw(js)]],
);
my %params = (
user_input => '',
);
print $tx->render_string(<
document.write('');
var user_input = '';
T
You'd better to consult the security experts on more complex cases.
=head2 How to interpolate structured texts into HTML without XSS
There's no silver bullet to parse structured texts in secure ways.
You'd better to consult the security experts to do so.
Some CPAN module might help you. See L for example.
=head2 How to manage localization in templates
You can register any functions including C, so no specific techniques are required.
For example:
use I18N::Handle;
# I18N::Handle installs the locale function "_" to the global namespace.
# (remember the symbol *_ is global)
I18N::Handle->new( ... )->speak('zh_tw');
my $tx = Text::Xslate->new(
function => {
_ => \&_,
},
);
Then in your templates:
See also: L, L.
=head2 How to load templates before Cing?
It is a good idea to load templates in preforking-model applications.
Here is an example to to load all the templates which is in a given path:
use File::Find;
my $path = ...;
my $tx = Text::Xslate->new(
path => [$path],
cache_dir => $path,
);
# pre-load files
find sub {
if(/\.tx$/) {
my $file = $File::Find::name;
$file =~ s/\Q$path\E .//xsm; # fix path names
$tx->load_file($file);
}
}, $path;
# fork and render ...
=head2 How to pre-process templates?
You can override C in Text::Xslate to process contents
before parsing them:
{
package MyTemplate;
use parent qw(Text::Xslate);
sub slurp_template {
my($self, $input_layer, $fullpath) = @_;
my $content = $self->SUPER::slurp_template(
$input_layer,
$fullpath,
);
### process $content ###
return $content;
}
}
The first argument I is an Text::Xslate instance; the second is
the argument of C option (default to C); the third
is the full path of a template file to load.
C may return both B and C.
Note that this hook is provided for pre-processing template contents, so you should call C to load templates into the memory.
=head1 SEE ALSO
L
L
L
=cut