ER wrote:
> After compiling a template, Is there a way to get a list of all
> variables that it references?
> It seems that this is something the compiler could keep track of if it
> doesn't already.

Hi ER (love your show on TV, BTW :-)

I've just checked in a patch that does this.

   http://template-toolkit.org/svnweb/Template2/revision/?rev=1258

It adds the TRACE_VARS option which you can use like this:

   my $tt       = Template->new( TRACE_VARS => 1 );
   my $template = $tt->template($name) || die $tt->error;
   my $vars     = $template->variables;

This returns a hash array containing variables names as keys.  The
values are hash arrays containing the keys of sub-items, and so on.

For example, a template like this:

    [% a; b.c; d.e.f %]

would return the following from the variables() method:

     {
         a => { },
         b => {
             c => { }
         },
         d => {
             e => {
                 f => { }
             }
         },
     }

You can introspect the variables from within the template if you like
(I'm not sure how useful this is, but I'm sure someone will think of a
good use for it)

     [% IF template.variables.a.b %]
        Fascinating!  This template references the a.b variable
     [% END %]

There are some basic tests in t/trace_vars.t.  There's no documentation and
it doesn't track SET variables, only GET (patches welcome).

It's officially marked as **experimental**, so don't shout at me if I decide
to completely change the way it works when I've had more time to think about
it properly (suggestions and patches welcome).

Cheers
A

_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates

Reply via email to