Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Template Toolkit output to a scalar?

by theguvnor (Chaplain)
on Jan 14, 2005 at 22:36 UTC ( #422416=perlquestion: print w/replies, xml ) Need Help??

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

I'm looking for the easiest way to redirect the output from TT2's process() method to a scalar, as opposed to STDOUT. Suggestions? (I am trying to avoid having to shell out to use tpage, if possible.)

Update: I looked at the template docs manual and did not see that. --me! Anyway, thanks for the lightning fast responses - I now see it in the perldoc page.

[Jon]

Replies are listed 'Best First'.
Re: Template Toolkit output to a scalar?
by Tanktalus (Canon) on Jan 14, 2005 at 22:42 UTC

    According to the Template docs, the third param to process can be a SCALAR ref - just pass in a ref to the variable you want to receive it.

Re: Template Toolkit output to a scalar?
by Ovid (Cardinal) on Jan 14, 2005 at 22:42 UTC
    #!/usr/bin/perl use strict; use warnings; use Template; my $data = { name => 'Ovid' }; my $template = Template->new; my $output; $template->process(\*DATA, $data, \$output); print $output; __DATA__ Hello, [% name %]

    Cheers,
    Ovid

    New address of my CGI Course.

Re: Template Toolkit output to a scalar?
by thor (Priest) on Jan 15, 2005 at 01:42 UTC
    In general, you can open a scalar as though it were a file.
    my $fake_file; open(my $fh, ">", \$fake_file) or die $!; print $fh, "foo\n"; print $fh, "bar\n"; print $fake_file;
    According to perldoc -f open, you can do this with STDOUT, you just have to close it first.

    thor

    Feel the white light, the light within
    Be your own disciple, fan the sparks of will
    For all of us waiting, your kingdom will come

      Now *that* is cool. Definitely hadn't stumbled on that tidbit. ++thor

      [Jon]

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://422416]
Approved by Ovid
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2020-04-08 18:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The most amusing oxymoron is:
















    Results (45 votes). Check out past polls.

    Notices?