On Mon, May 27, 2002 at 07:58:45AM +0200, Uwe Voelker wrote:
> I'm coming from HTML::Template - there is a dot no problem. Is there a 
> way to make the dot working in TT too?

I'm afraid not.  '.' is a reserved character and can't be used in
variable names.  It may be an annoying limitation but the meaning of
'.' is absolutely central to TT and can't be subverted.  The same is
true of most programming languages.  Off the top of my head I can't
think of one that allows you to use '.' inside a variable name.

I'm guessing that HTML::Template uses simple string substitution in 
resolving variables so can accept pretty much any character sequence.

Having said all that, you can bodge TT into working by using a similar
approach to the example you posted.  If you define a variable like this:

  [% x = 'foo.bar' %]
  [% $x %]

Then it doesn't work.  The generated Perl code looks like this:

  $stash->get($stash->get('x'));

which is the same as:

  $stash->get('foo.bar');

The get() method sees the '.' in the name and parses it into a list
like so: [foo => 0, bar => 0].  This causes it to look for 'bar' in 
a hash called 'foo' which doesn't exist.

However, if you nest the data inside a hash then it does work.

  [% y = 'foo.bar' %]
  [% x.$y %]

The generated Perl code instead looks like this:

  $stash->get([ x => 0, $stash->get('y'), 0]);

The get() method now gets the variable pre-parsed into an array and it
doesn't perform any additional parsing on the elements.

Below is an example.  While you can use this to work around your existing
data, I wouldn't recommend such hackery unless you really have to :-)

  #!/usr/bin/perl -w
  use strict;
  use Template;

  my $tt   = Template->new();
  my $vars = { data => { 'FORM.Table.0.name' => 'uwe' } };
  $tt->process(\*DATA, $vars) || die $tt->error();

  __DATA__
  [% var = 'FORM.Table.0.name' %]
  Hello [% data.$var %]

Output:
  Hello uwe



HTH
A




Reply via email to