[Templates] how do I find all parsed tags?

Blue Eyed Devil eyed_devil_blue at hotmail.com
Tue Jul 1 13:15:48 BST 2008


 
 Hi Andy,
 
 It's always an honor when I get to have a direct communication link with the developer(s) of any technologies that I choose to use (this is not to undermine any other contributors that reply of course, but you know what I mean ;-)
 
 It sounds like you've been busy in the lab because from what you've explained of what TT3 will provide us, it sounds like it's going to have some great features (not to mention some internal code for us to feast on).
 
 I looked at your example where you have @free_vars, and this is exactly what I'm looking for. I'm a bells-and-whistles guy myself so I like to pack my stuff with switches, etc... in light of this I was thinking you might even make a switch that would say "ignore_free_vars" that if set to true it would leave the tags untouched in the file if it has not been resolved/defined by the time the template is parsed. Either way, as long as there will be the free_vars concept that will give people with more specialized requirements some more control without having to mess with the internals (which can be disasterous if left to inexperienced but ambitious hands).
 
 To get @all_vars one could simply take passed in @vars and combine it with @free_vars. I don't think an @all_vars would then be 'necessary', but might be another small bell or whistle.
 
 The AST I like. Anything abstract is my domain and this is what I generally promote (most often with much resistance by peers may I add) for the sake of maximum flexibility. It even sounds like your AST might give a performance boost. It sounds so powerful it's scary because of all the doors this opens up in my imagination.
 
 So now that I see TT3 will support what I'm after (may I add I'm excited to hear that), having touched on performance boost I'm sure TT3 will tackle this too. Like I said in my other posts, I'm using TT2 for my www.perlscript.com, and I will put a "powered by TT" once I'm happy with how everything works (on my end) - I truly hope TT3 will be backward compatible with TT2 (but if it isn't it's okay because that will put my framework to the test to see if my abstract layer works as it should)... I was going to write my own parser but then decided not to reinvent the wheel and really have the website be a working example of Open Source PERL work coming together. 
 
 Thanks for a great product Andy!
 
 Milan
 
> Date: Tue, 1 Jul 2008 12:44:03 +0100> From: abw at wardley.org> To: eyed_devil_blue at hotmail.com> CC: moseley at hank.org; templates at template-toolkit.org> Subject: Re: [Templates] how do I find all parsed tags?> > Blue Eyed Devil wrote:> > Maybe other people can think of other uses for this to where maybe the > > developers of TT would consider implementing this relatively simple feature.> > Hi Milan,> > It may be a relatively simple feature, but implementing it is decidedly> non-trivial :-)> > It's not possible in TT2 without hacking a new parser grammar (see> parser/Parser.yp in the TT2 dist) and/or a new Template::Directives.> Even then, I think you would be very limited in what you can do.> > The lack of flexibility in the TT2 parser is one of the main things to change> in TT3. I've completely re-written the parser (several times, actually) to> provide much greater flexibility in a) the range of languages that it can> parse, and b) what you can do with the output that it generates.> > The TT3 parser compiles the template to an AST (Abstract Syntax Tree). The> usual modus operandi for TT2 is to compile templates to Perl code. You> achieve that in TT3 by having a Perl generator walk the AST and convert it> to Perl code. However, the TT3 AST tree is also directly executable (i.e.> given any AST node, you can call $node->value($context) to evaluate it in a> given context) so you don't need to convert it to Perl if you don't want to.> > Once you've got the parsed template in AST form, you can do pretty much> anything you like with it. For example, you can transform it to a target> language (e.g. Perl, Parrot, Javascript, etc), you can pretty-print it as HTML> for debugging, or you can analyse it to trace static dependencies on> variables, templates, etc. Now, you can't trace all dependencies because of> the dynamic nature of TT wrt to variables (e.g. foo.$var) and templates> (INCLUDE "example/$file"), but you can locate the static resources referenced> in a template (and recursively, any templates that it depends on) and at the> very least detect the use of dynamic resources and track what variables> they rely on (e.g. 'var' in 'foo.$var' and 'file' in 'example/$file'. So you> can certainly get a useful 90% of the way there (or 100% if you don't use> dynamics).> > I haven't implemented this last bit yet, but it's next on my TODO list (well,> OK, it's actually item #3, but close enough). I'm planning to implement a> partial evaluation method for the AST node that will evaluate those parts of> the tree that it can and leave the rest unresolved. Something like this:> > use Template v3; # RSN> > # fetch a template> my $template = Template->template('example.tt3');> > # partially process template> $template->partial( name => 'Arthur Dent', site => $my_site );> > # now all 'name' and 'site' references have been resolved (except any> # like 'site.$thing' where we haven't got a 'thing' yet)> > As a side-effect, performing a partial evaluation will also detect the use> of any variables, templates or other resources that can't be resolved yet> (the free variables, or more generally, free resources).> > The end result is that templates will be able to tell you what parameters> they're expecting to be fed. You could then prompt a user to enter values for> these variables, for example. Something like this:> > # see what variables are left to be filled> my @free_vars = $template->free_var_names;> > # prompt the user to provide values> my $vars = { }; > foreach my $var (@free_vars) {> $vars->{ $var } = prompt("$var: ");> }> > # now process template> print $template->expand($vars);> > Does that sound like the kind of thing you're after?> > Most of the parser and AST code is written, but the AST visitors and partial> evaluation are still TODO. I'm hoping to get that done in the next few weeks> but realistically, it'll be at least another month or two before there's> enough of TT3 working to play with. So although it doesn't help you much now,> it may be reassuring to know that there'll be more scope to monkey around with> the template innards in TT3.> > A
_________________________________________________________________
Use video conversation to talk face-to-face with Windows Live Messenger.
http://www.windowslive.com/messenger/connect_your_way.html?ocid=TXT_TAGLM_WL_Refresh_messenger_video_072008
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.template-toolkit.org/pipermail/templates/attachments/20080701/d3daf35c/attachment.htm 


More information about the templates mailing list