Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template::Toolkit variable processing

I'm using TT from perl, and trying to insert into template and process a variable, that itself contains TT directives.

So, in the script I'm writing smth like:

$var{descr} = "[% pid = 1; INSERT plink.par %]";

And then in TT template (which received \%var):

[% BLOCK parsedDescr %]
[% descr %]
[% END %]
<p>[% INCLUDE parsedDescr %]

And I'm expecting pid variable being set to "1", and plink.par file inserted. But instead I get on my html page exactly contents of the variable descr:

[% pid = 1; INSERT plink.par %]

i.e., this variable stays unprocessed by TT.

How do I make TT process it's contents?

like image 922
Sly Avatar asked May 22 '12 22:05

Sly


People also ask

How do I add a variable to a template?

Choose Template > New Variable from the editor toolbar (or choose an existing variable to add it to the page) Enter a name for the variable. Press Enter (by default this will create a single-line text input field)

What are variables in templates?

A variable template may be introduced by a template declaration at namespace scope, where variable-declaration declares a variable. When used at class scope, variable template declares a static data member template.

Which keyword is used to set a variable in the template?

In the template, you use the hash symbol, # , to declare a template variable. The following template variable, #phone , declares a phone variable with the <input> element as its value.

Which directive is used to embed the section into a template?

Template Inheritance The @section directive, as the name implies, defines a section of content, while the @yield directive is used to display the contents of a given section.


1 Answers

You can use the eval filter:

<p>[% descr | eval %]

From the linked manpage:

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.

like image 160
Eugene Yarmash Avatar answered Oct 12 '22 09:10

Eugene Yarmash