Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

"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:

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.

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.

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://704713]
Approved by kyle
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2018-06-23 06:53 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.