http://www.perlmonks.org?node_id=156789


in reply to Dynamically altering static TT templates

One way would to be to use the Decorator pattern and write a wrapper object around the Template toolkit object that appends or prepends any information you need. You can then add additional configuration information specific to this additional object to control it much more closely.

This decorator object can of course contain its own instance of a TT parser so you can use the same syntax where you wish. The advantage to this is that the output it produces is applied to either the top or the bottom of the template regardless of whether the designers remove your comment syntax.

Of course this only works for the whole template and you want it for each. Taking a look at the docs for the Template toolkit it looks like, to me anyway, that the class you should consider is Template::Context, specifically the include and process functions. One method to play with here is to write your own context object and override those functions so that you may add the bits and bobs you want for your template vars and then call the method from the parent class to do the actual parsing.

Though the effort involved in getting the parser to call your context object instead of the one supplied may not be worth it. Of course you could just replace these functions by hacking into the namespace but that would be just plain nasty :)

Update

I had a quick play with context.pm and I managed to get it to return the processed filename by changing the last line in the process() routine to:
return $output . $template->[0]{'name'};
This works because the $template variable passed to process is a document object. We have to mess about with the anonymous array because the routine automatically puts the object into one further up in the code.

I haven't played with include() but it looks to operate in the same way. Its trivial then to use HTML comments to hide this additional output.

Warning

I do not recommend trying to repeat what I did unless you know exactly what you are doing. At best you'll have to reinstall the module, at worst you won't know you need to reinstall it till you do that demo for the important client you have been preparing for all week and everything dies :).