[Catalyst] Capture template output?

Jason Kohles email at jasonkohles.com
Sun Feb 19 19:14:33 CET 2006


On 2/19/06, Mark Blythe <perl at markblythe.com> wrote:
> Is there a way to capture the output of rendering a template through
> the view rather than sending it back as the request body?  It seems
> like this should be possible, but I haven't found any mention in the
> manual.
>
The TT view doesn't send the rendered output to the browser itself, so
after you have forwarded to a TT view, you can just pull the results
of the rendering from $c->response->body.

sub some_email_controller : Private {
    my ( $self, $c ) = @_;

    $c->stash->{template} = 'email_template.tt2';
    $c->forward('MyApp::View::TT');
    open(MAIL, "| sendmail -t -oi -oem -odq");
    print MAIL $c->response->body;
    close(MAIL);
}

I usually like to setup a separate view for this, similar to the way
that the TTSite helper automatically wraps HTML headers and footers
around the page, you can setup a TT view just for email rendering that
wraps email headers and signatures.  I could probably extract some of
my old view code for that and turn it into a self-contained demo if
anyone is interested, I generally don't do it this way anymore, as I
like to separate these functions out into a separate library so I can
send email from automated process and non-Catalyst code too...

--
Jason Kohles
email at jasonkohles.com - http://www.jasonkohles.com/
"A witty saying proves nothing."  -- Voltaire



More information about the Catalyst mailing list