agianni has asked for the wisdom of the Perl Monks concerning the following question:

Is there an easy way to do dynamic variable naming in Template Toolkit a la:

${ eval $var_name } = 'foo';

I know I can do this by using a [% PERL %] block, but I'd rather not. I also noticed that there is an eval filter built into TT, but that appears to be for eval-ing TT code.

And, lest anyone suggest that this is what hashes are for :) I'm well aware of that fact, but I need to get this working to provide a hook into some legacy template code.

perl -e 'split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(joi +n(q{},map{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79 +*36997,13*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));'

Replies are listed 'Best First'.
Re: Dynamic variable naming in Template Toolkit
by perrin (Chancellor) on May 30, 2007 at 20:14 UTC

      Right, I've read it. What I want to be able to do based on my understanding of the docs is something like:

      [% var_name = 'foo'; $var_name = 'bar'; # I wish $foo would eq 'bar' but it doesn't %]

      but my understanding is that:

      [% var_name = 'foo' %]

      is the same as:

      [% $var_name = 'foo' %]

      Right? So far I'm thinking that I can't achieve what I want. Fortunately this is Perl, so there's always another way :)

      perl -e 'split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(joi +n(q{},map{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79 +*36997,13*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));'
        Try ${var_name} = 'bar', and if that doesn't work, ask on the TT mailing list. I think TT supports this.
Re: Dynamic variable naming in Template Toolkit
by Trizor (Pilgrim) on May 30, 2007 at 19:32 UTC

    Are you positive you need symbolic references? I'm pretty sure there is no support for them in TT. How is TT being invoked? From perl you pass it a vars hashref, when constructing this you can do the symbolic dereference there. Or if thats not an option why not try and pre-populate a hash instead of using symbolic references? Theres no real easy way to do this that I'm aware of.

      How is TT being invoked?

      I could override the method that invokes the template for me (I'm using CGI::Prototype) but I'd rather not.

      why not try and pre-populate a hash instead of using symbolic references?

      That's actually what I'm doing... to start at least. There is already a large data structure being passed into the template. I'm adding an extra key to the data structure with all of the additional variables I need as keys. Then in the template, I want to turn those into simple scalar variables. Something like this:

      $ds->{for_template}->{this_key} = 'foo'; $ds->{for_template}->{that_key} = 'bar';

      Where $ds is the data structure that is already being passed into the template. Then in the template (in perl for now):

      for my $var ( keys %{ $ds->{for_template} } ){ ${eval $var} = $ds->{for_template}->{$var}; }

      Basically, I want to flatten the hash into individual variables that the template is expecting to be defined.

      perl -e 'split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(joi +n(q{},map{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79 +*36997,13*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));'