Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Template Toolkit on a string instead of a file?

I have some strings that I am pulling out of a database and I would like to use Template Toolkit on them, but I can't seem to figure out how to use strings as TT input. Any tips?

Thanks!

-fREW

like image 799
Frew Schmidt Avatar asked Dec 03 '22 16:12

Frew Schmidt


2 Answers

The documentation explains:

process($template, \%vars, $output, %options)

The process() method is called to process a template. The first parameter indicates the input template as one of: a filename relative to INCLUDE_PATH, if defined; a reference to a text string containing the template text; ...

       # text reference
       $tt->process(\$text)
           || die $tt->error(), "\n"
like image 87
oeuftete Avatar answered Mar 08 '23 10:03

oeuftete


From the docs:

# text reference
$text = "[% INCLUDE header %]\nHello world!\n[% INCLUDE footer %]";
$tt->process(\$text)
    || die $tt->error(), "\n";

(Looks like I should have refreshed the page before posting.)

like image 40
Blank6 Avatar answered Mar 08 '23 12:03

Blank6