[Templates] how do I find all parsed tags?

Sean McAfee eefacm at gmail.com
Mon Jun 30 18:46:17 BST 2008


On Mon, Jun 30, 2008 at 10:11 AM, Blue Eyed Devil <
eyed_devil_blue at hotmail.com> wrote:

>  To reiterate my example:
>
>  [% TAG %] is a [% TYPE %] that can be [% WORD %]
>
>  If I don't pass any vars to process(), there should be a collection,
> property, or method I could call that would work something like:
>
>  $template->process(....);
>  @all_tags = $template->tags();
>

I still don't really understand how this is meant to be useful, but you
could capture all of the tag contents by providing a custom Template::Parser
object that overrides the split_text() method.

Not-fully-tested code:

package MyParser;

our @ISA = qw(Template::Parser);

sub split_text {
  my ($self, $text) = @_;
  my $tokens = $self->SUPER::split_text($text);
  ref eq 'ARRAY' and ++$self->{alltags}{$$_[0]} for @$tokens;
  return $tokens;
}

sub tags {
  my $self = shift;
  return keys %{ $self->{alltags} };
}

And then:

my $parser = MyParser->new;
my $template = Template->new(PARSER => $parser);
$template->process(<whatever>);

my @all_tags = $parser->tags;


--Sean
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.template-toolkit.org/pipermail/templates/attachments/20080630/6956f4df/attachment.htm 


More information about the templates mailing list