Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
We don't bite newbies here... much
 
PerlMonks  

"Evaluating" TemplateToolkit variables.

by techcode (Hermit)
on Aug 16, 2008 at 18:04 UTC ( #704713=perlquestion: print w/ replies, xml ) Need Help??
techcode has asked for the wisdom of the Perl Monks concerning the following question:

UPDATE-START:
I found out that eval does almost what I asked - except you need to include | eval which I would like to avoid if possible.
I looked before posting here and I didn't see - it seems that meerly asking a question unblinded me somehow - and this isn't the first time either.
The eval filter evaluates the block as template text, processing any directives embedded within it. This allows template variables to contain template fragments, or for some method to be provided for returning template fragments from an external source such as a database, which can then be processed in the template as required.
UPDATE-END.

I need a way to evaluate/process TemplateToolkit variables from within a TT template. As in case of Process/Include when stuff "inside" is processed.

So in variable I have something like this, and it could also contain other TT directives:

Copyright 2008 ([% site_name %]).
In reality value for that is coming from a DB - so I can't simply place that in a BLOCK or external file.

Ideas I have so far - but honestly, non of the solutions is optimal:
  • move variable value to a real file and then use INCLUDE/PROCESS

    It would mean you need to change the original template to have that PROCESS whatever, and there is a problem of values that don't exist (not yet entered or simply not used) as I would need to create empty files for them so I don't get errors. On top of having to manage files in the first place.

  • Create two TT objects, process all such variables with one in a loop (no wrapper and all), and then just do as normal in the other (wrapper and all).

    Loop is already there due to other things - though 3rd is better variant of this it seems.

  • Run TT processing two times, first run to do the real processing (include wrapper ...etc) and then the second to process the stuff read from variables (if any such variables).

    This one seems best of the 3 - quite easy to implement, but will probably hurt performance somewhat and screw up TT built in caching. Though I might implement my own caching anyway.

Another idea is to change TT and extend it so it can read BLOCK content from a DB - but honestly - I'd like to skip this one due to lack of available time.

Have you tried freelancing? Check out Scriptlance - I work there. For more info about Scriptlance and freelancing in general check out my home node.

Comment on "Evaluating" TemplateToolkit variables.
Download Code
Re: "Evaluating" TemplateToolkit variables.
by kyle (Abbot) on Aug 16, 2008 at 19:16 UTC

    This came up recently in TT: interpolating again and again. The only answers given were the one you've already found (eval/evaltt). If that's not tasty enough, maybe you could write a plugin that does what you want.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://704713]
Approved by kyle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2014-04-19 15:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (482 votes), past polls