Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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 :).

In reply to Re: Dynamically altering static TT templates by simon.proctor
in thread Dynamically altering static TT templates by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2021-12-04 23:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (30 votes). Check out past polls.

    Notices?