Template-Sandbox
view release on metacpan or search on metacpan
lib/Template/Sandbox.pm view on Meta::CPAN
Note that you can only add "top-level variables", that is you can do
the first of these but not the second:
$template->add_var( 'user' => { profile => $profile, }, ); # Works.
$template->add_var( 'user.profile' => $profile ); # Wrong!
=item B<< $template->add_vars( >> I<$vars> B<)>
Adds a I<template variable> with name and value from each key and value
of the hashref I<$vars>.
Like C<< $template->add_var() >>, this can only add top-level variables.
=item B<< $template->merge_var( >> I<$name>, I<$value> B<)>
Merges the contents of I<$value> into the I<template variable> named
I<$name>.
How the merge is performed depends on the nature of of I<$value>:
=over
=item I<$value> is a scalar
If the named I<template variable> does not already exist, it is set to
I<$value>. If the variable already has a value, it remains unchanged.
=item I<$value> is an arrayref
If I<$value> is an arrayref, then the contents of the arrayref are pushed
onto the arrayref contents of the I<template variable>, or assigned if
no arrayref already exists.
=item I<$value> is a hashref
Each key and value of I<$value> is merged with each key and value of
the hashref in the named I<template variable>.
=back
If this seems a little complicated, think of it that arrayref variables
get appended to, and hashrefs "have any missing entries filled in":
# In one part of your app:
$template->merge_var(
stylesheets => [ 'login_widget.css' ],
);
# Then elsewhere:
$template->merge_var(
stylesheets => [ 'search.css', 'advertising.css' ],
);
# Contents of 'stylesheets' is now:
[ 'login_widget.css', 'search.css', 'advertising.css' ]
# Or a more complicated (and contrived) example:
$template->merge_var(
userprefs => {
private_messages => {
message_order => 'oldest-first',
delete_when_viewed => 1,
fave_tags => [ 'music', 'video' ],
},
},
);
$template->merge_var(
userprefs => {
private_messages => {
delete_when_viewed => 0,
friends_only => 1,
fave_tags => [ 'computers' ],
},
},
public_messages => {
message_order => 'newest-first',
},
);
# Contents of 'userprefs' is now:
{
private_messages =>
{
message_order => 'oldest-first',
# This already existed and remained unchanged.
delete_when_viewed => 1,
# This didn't exist and was added.
friends_only => 1,
# This already existed and was appended to.
fave_tags => [ 'music', 'video', 'computers' ],
},
# This didn't exist and was added.
public_messages =>
{
message_order => 'newest-first',
},
}
=item B<< $template->merge_vars( >> I<$vars> B<)>
For each key and value in the hashref I<$vars>, perform a
C<< $template->merge_var() >> with that key and value.
=item B<< $template->clear_vars() >>
Clears all I<template variables> that have been added so far, as if
no I<template variables> had been added at all.
This method added in version 1.01_11 of L<Template::Sandbox>.
=item B<< $template->run() >>
Runs the template, returning a reference to the output.
C<< $template->run() >> will I<always> return a valid string reference,
or raise an exception trying: even if no output is produced a reference
to the empty string will be returned, so the following is safe (if ugly):
print ${$template->run()};
=item B<< $template->dumpable_template() >>
Returns a somewhat human-readable dump of the compiled template program,
this probably isn't very useful unless you're me, or doing me the kindness
of debugging something for me. :)
=back
=head1 TEMPLATE SYNTAX
With the exception of I<compile defines> (detailed below in
L</"Compile Defines">), all L<Template::Sandbox> syntax is written
as statements enclosed within
C<< <: >> and C<< :> >> symbols, for example:
<: if a :>some content<: else :>some other content<: endif :>
<: for x in y :>some loop content<: endfor :>
Everything outside the C<< <: :> >> delimiters is considered to be
template content and will be reproduced unaltered in the template's
( run in 2.500 seconds using v1.01-cache-2.11-cpan-8f98c5d2c55 )