Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Accessing Template Toolkit Variables

by Util (Priest)
on Sep 30, 2007 at 17:05 UTC ( #641787=note: print w/ replies, xml ) Need Help??


in reply to Accessing Template Toolkit Variables

The documentation says "The Template::Stash module is used to fetch and store template variables.", so I (a recent TT convert) thought your code should work. I did some experimenting:

use strict; use warnings; use Template; use Template::Stash; my $template_string = <<'END_OF_TEMPLATE'; [% x1 = "x1_defined_in_template" -%] Inside the processed template: x1 = [% x1 %] x2 = [% x2 %] x3 = [% x3 %] END_OF_TEMPLATE my $stash = Template::Stash->new(); my $tmpl = Template->new( STASH => $stash ); $stash->set( x1 => 'x1_defined_in_stash_set' ); $stash->set( x2 => 'x2_defined_in_stash_set' ); $stash->set( x3 => 'x3_defined_in_stash_set' ); my $t_vars = { x3 => 'x3_defined_in_process_t_vars' }; # Process and print the template $tmpl->process( \$template_string, $t_vars ) or die $tmpl->error(); print "Post-process, the stash vars are:\n"; print ' x1: ', $stash->get('x1'), "\n"; print ' x2: ', $stash->get('x2'), "\n"; print ' x3: ', $stash->get('x3'), "\n"; print "Note that we could check more easily via _dump:\n"; print $stash->_dump(), "\n";
Output:
Inside the processed template: x1 = x1_defined_in_template x2 = x2_defined_in_stash_set x3 = x3_defined_in_process_t_vars Post-process, the stash vars are: x1: x1_defined_in_stash_set x2: x2_defined_in_stash_set x3: x3_defined_in_stash_set Note that we could check more easily via _dump: [Template::Stash] { x3 => x3_defined_in_stash_set x2 => x2_defined_in_stash_set global => { } inc => CODE(0x18503f8) dec => CODE(0x1850458) x1 => x1_defined_in_stash_set _PARENT => <undef> }

As the Anonymous Monk said, the stash is reverting to its prior state after process(). So, unless you use the stash to communicate *all* variable state, your approach does not look workable.

I also tried the method listed in the "Show All Variables" thread on the TT mailing list, but I could not make it work at all; perhaps the TT code no longer recognizes a bare 'keys' statement as referring to the whole stash.

Since Template::Stash does handle all the variable get&set, you might be able to cache the last get|set for each variable as Template::Stash handles it via subclassing:

use strict; use warnings; package Template::Capturing::Stash; { use base 'Template::Stash'; our %remembering_hash; sub get { die if @_ != 2; my ( $self, $var_name ) = @_; my $var_value = $self->SUPER::get($var_name); #print "! Get of $var_name = $var_value\n"; $remembering_hash{$var_name} = $var_value; return $var_value; } sub set { die if @_ < 3 or @_ > 4; my ( $self, $var_name, $var_value, $default ) = @_; die "Not yet coded to handle default" if defined $default; my $return_value = $self->SUPER::set( $var_name, $var_value ); #print "! Set of $var_name = $var_value\n"; $remembering_hash{$var_name} = $var_value; return $return_value; } } package main; use Template; use Template::Stash; my $template_string = <<'END_OF_TEMPLATE'; [% x1 = "x1_defined_in_template" -%] Inside the processed template: x1 = [% x1 %] x2 = [% x2 %] x3 = [% x3 %] END_OF_TEMPLATE my $stash = Template::Capturing::Stash->new(); my $tmpl = Template->new( STASH => $stash ); $stash->set( x1 => 'x1_defined_in_stash_set' ); $stash->set( x2 => 'x2_defined_in_stash_set' ); $stash->set( x3 => 'x3_defined_in_stash_set' ); my $t_vars = { x3 => 'x3_defined_in_process_t_vars' }; my $out; $tmpl->process( \$template_string, $t_vars, \$out ) or die $tmpl->error(); print $out; #print $stash->_dump(), "\n"; use Data::Dumper; $Data::Dumper::Sortkeys=1; print Dumper( \%Template::Capturing::Stash::remembering_hash );
Output:
Inside the processed template: x1 = x1_defined_in_template x2 = x2_defined_in_stash_set x3 = x3_defined_in_process_t_vars $VAR1 = { 'component' => '', 'x1' => 'x1_defined_in_template', 'x2' => 'x2_defined_in_stash_set', 'x3' => 'x3_defined_in_process_t_vars' };
While this looks *very* promising, it might be limited to only simple variable access. I did not have time to test it on complex (dotted) variable access, where this technique might break down.

If you figure out a better solution, or get one from another venue, please post it here for future Monks!


Comment on Re: Accessing Template Toolkit Variables
Select or Download Code
Re^2: Accessing Template Toolkit Variables
by ktowle (Initiate) on Oct 06, 2011 at 19:48 UTC

    Extended the module to at least nominally handle dotted variables:

    use strict; use warnings; package Template::Capturing::Stash; { use base 'Template::Stash'; our %remembering_hash; sub get { die if @_ != 2; my ( $self, $var_name ) = @_; my $var_value = $self->SUPER::get($var_name); if ( ref $var_name eq 'ARRAY' ) { $var_name = compound_var( @{$var_name} ); } #print "! Get of $var_name = $var_value\n"; $remembering_hash{$var_name} = $var_value; return $var_value; } sub set { die if @_ < 3 or @_ > 4; my ( $self, $var_name, $var_value, $default ) = @_; die "Not yet coded to handle default" if defined $default; my $return_value = $self->SUPER::set( $var_name, $var_value ); if ( ref $var_name eq 'ARRAY' ) { $var_name = compound_var( @{$var_name} ); } #print "! Set of $var_name = $var_value\n"; $remembering_hash{$var_name} = $var_value; return $return_value; } sub compound_var { my @var_name = @_; my @compound_var; foreach my $element ( @var_name ) { unless ( $element eq '0' ) { if ( ref $element eq 'ARRAY' ) { $element = compound_var( @{$element} ); } push @compound_var, $element; } } return join '.', @compound_var; } } 1;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://641787]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2014-07-30 02:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls