Question

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

Was it helpful?

Solution

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"

OTHER TIPS

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.)

You may find String::TT as a nicer alternative way of doing it. 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 %]';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top