On Sun, Apr 5, 2009 at 3:23 PM, John Bida <[email protected]> wrote:
> Is there a way to use the INCLUDE directive but only copy specific
> variables from the stash? I want to limit the global variables the
> included template sees.
>
Not out of the box. One of the cleaner ways to accomplish that that I can
think of is to define a subclass of Template::Stash with a custom clone()
method that recognizes a special "retain" parameter:
package My::Stash;
use base qw(Template::Stash);
sub clone {
my ($self, $params) = @_;
if (keys %$params == 1 && defined(my $retain = $params->{retain})) {
my $clone = bless { }, ref $self;
@$clone{ @$retain } = @$self{ @$retain };
$clone->{_PARENT} = $self;
return $clone;
} else {
return $self->SUPER::clone($params);
}
}
Then if you instantiate your template object like so:
my $template = Template->new({ STASH => My::Stash->new });
...you can write a template like this:
[% BLOCK showem %]
foo = [% foo %], bar = [% bar %], baz = [% baz %]
[% END %]
[% INCLUDE showem retain = [ 'foo' ] %]
[% INCLUDE showem retain = [ 'foo', 'bar' ] %]
[% INCLUDE showem %]
Season the implementation to taste.
--Sean
_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates