Rendering TT on command with Catalyst

user-pic

Wiki Extras for this post

Scenario

You're trying to send an email (via Catalyst::Plugin::Email, MIME::Lite, Email::Sender::Simple, your choice) and you would like to be able to stuff your canned email messages into their respective Template::Toolkit templates. One way to do this is use Catalyst::View::Email::Template, which is pretty cool, but forces you to use Catalyst::View::Email, which some people aren't too keen on. So, you can do something like this:

sub email_stuff : Local {
    my ($self, $c, $review_id) = @_;

    $c->stash(
        review_url    => $c->uri_for("/thingy/${review_id}"),
        status_msg => "Thank you for your feedback!",
        template      => 'view_all.tt2',
    );

    $c->email(
        header => [
            From    => $c->config->{'from'},
            To      => $c->config->{'to'},
            Subject => "Emaily stuff!",
        ],
        body => $c->view('TT')->render($c, 'review.tt2'),
    );

    return; 
}

The line $c->view('TT')->render($c, 'review.tt2') allows you to have your view render your template for you using all the things you put into $c->stash. Pretty cool eh?

No TrackBacks

TrackBack URL: http://www.catalyzed.org/mt/mt-tb.fcgi/70

2 Comments

| Leave a comment

dont forget to trap errors.

my $body = $c->view('TT')->render( $c, 'alerts/add_new.tt' );
if ( ref($body) eq 'Template::Exception' ) { die $body->as_string; }

Good catch! Thanks for that.

Leave a comment

All comments are moderated. Spammers don't waste your time

Sponsored By


Ionzero: Rescue your dev project.
OpenID accepted here Learn more about OpenID

Following

Not following anyone

Note to spammers: all comments are moderated. Don't waste your time