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

I have a few lines that I exit the database and I would like to use the Template Toolkit for them, but I cannot figure out how to use the lines as input to the TT. Any tips?

Thanks!

-fREW

+4
source share
3 answers

The documentation explains:

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

The process () method is called to process the template. The first parameter specifies the input pattern as one of: the file name relative to INCLUDE_PATH, if specified; a link to a text string containing the text of the template ; ...

# text reference $tt->process(\$text) || die $tt->error(), "\n" 
+11
source

From docs :

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

(It looks like I should refresh the page before posting.)

+4
source

You can find String :: TT as the best alternative way to do this. Some teasers from the pod ...

 use String::TT qw/tt strip/; sub foo { my $self = shift; return tt 'my name is [% self.name %]!'; } sub bar { my @args = @_; return strip tt q{ Args: [% args_a.join(",") %] } } 

and...

 my $scalar = 'scalar'; my @array = qw/array goes here/; my %hash = ( hashes => 'are fun' ); tt '[% scalar %] [% scalar_s %] [% array_a %] [% hash_h %]'; 
+2
source

All Articles