pileofrogs has asked for the wisdom of the Perl Monks concerning the following question:

Blessings upon ye, Monks of Perl.

I recently embarrassed myself by asking TT & templates from data?. And here is another, hopefully less stupid question:

Is there a way to make Template::Toolkit process the contents of a variable inside of a template?

E.G.

my $vars = { header => 'hello, [% name %]', footer => 'goodbye, [% name %]', name => 'Dave', } my $template = <<"END"; [% header %] I'm afraid I can't do that [% footer %] END my $TT = Template->new(); my $output; $TT->process(\$template,$vars,\$output) || die $TT->error();

If "header" and "footer" were external files, I could INCLUDE them, but they are scalars.

I was able to make it work by running process twice:

$TT->process(\$template,$vars,\$output) || die $TT->error(); $TT->process(\$output,$vars,\$output) || die $TT->error();

But I'd rather do something less kludgey and more scalable.

Thanks for any ideas!
--Pileofrogs

Replies are listed 'Best First'.
Re: Yet Another Stupid TT Question
by perrin (Chancellor) on Dec 12, 2008 at 21:50 UTC
    The "eval" filter does what you want.

      Yes!

      This is exactly what I wanted!

      Thank you!!

      What's that? I can't find a module or directive by that name.
        It's a filter. Check the filters doc.
Re: Yet Another Stupid TT Question
by jeffa (Bishop) on Dec 12, 2008 at 21:17 UTC

    I recommend that you use a WRAPPER instead of a footer and header. That might just fix your problem right there.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Yet Another Stupid TT Question
by ikegami (Patriarch) on Dec 12, 2008 at 21:21 UTC

    If you had two templates, you could do

    layout:

    <p>hello, [% name %] [% content %] <p>goodbye, [% name %]

    somepage:

    <title>Some Page</title> [% WRAPPER layout %] <p>This is the body of the page. [% END %]

    Process "somepage".

      Except the whole point is that I don't. I've got some dynamically generated text that may include more TT markup, which may itself include more TT markup. That's not a problem if the text resides in a text file, but it doesn't seem to work if it lives in a scalar. That seems odd to me.

        Re: Template Toolkit Cache should do the trick. It allows templates to be read from memory instead of from disk.

        Update: Some code:

        use Template qw( ); use Template::Provider::Memory qw( ); my %mem_drive = ( 'layout' => [ time(), <<'__EOI__' ], <p>hello, [% name %] [% content %] <p>goodbye, [% name %] __EOI__ 'somepage' => [ time(), <<'__EOI__' ], <title>Some Page</title> [% WRAPPER layout %] <p>This is the body of the page. [% END %] __EOI__ ); my $tt = Template->new({ LOAD_TEMPLATES => [ Template::Provider::Memory->new({ MEM_DRIVE => \%mem_drive }), Template::Provider->new(), ], }); my $vars = { name => 'Dave', }; my $output; $tt->process('somepage', $vars) or die($tt->error(), "\n");

        The module and the var need a better name.