[Templates] how do I find all parsed tags?
Blue Eyed Devil
eyed_devil_blue at hotmail.com
Mon Jun 30 19:15:03 BST 2008
Yeah that sounds like an simple and elegant solution (I haven't tested the code, but the thought process looks suitable). I had hoped there was something in TT that I might have overlooked that gives us access to the tokens, etc... since I only looked under TT's hood for the first time last night. I have to admit for some reason I wasn't thinking overriding the split_text(). I guess everyone has right to a blonde moment :-)
I'm going to play with the solution I'm working on now to see if it will work, and if not, at least I have a new lead I can follow. Thanks Sean!
If anyone else has other ideas, feel free to contribute.
The reasons as to why a particular feature is desired/needed may not be evident until the need presents itself. This would be one of such times.
Maybe something along the lines (semi-pseudocode):
...
my @all_tags = $parser->tags;
$output .= $_->new->execute() for @tags;
Come to think of this particular example I'd actually put this as part of the process() so it could properly replace the tags with their corresponding values.
Once my website is ready I can't wait to put up the "Powered by TT" :-) Not to mention crediting other Perlscripters ! If anyone is interested in further contributing to the development of perlscript.com, let me know! Ideas, comments, suggestions, etc... are always welcome.
Milan
Date: Mon, 30 Jun 2008 10:46:17 -0700From: eefacm at gmail.comTo: eyed_devil_blue at hotmail.comSubject: Re: [Templates] how do I find all parsed tags?CC: templates at template-toolkit.orgOn 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
_________________________________________________________________
Need to know now? Get instant answers with Windows Live Messenger.
http://www.windowslive.com/messenger/connect_your_way.html?ocid=TXT_TAGLM_WL_Refresh_messenger_062008
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.template-toolkit.org/pipermail/templates/attachments/20080630/92a2f475/attachment-0001.htm
More information about the templates
mailing list