From: Simon Elliott Date: Wed, 8 Aug 2007 17:30:04 +0000 (+0000) Subject: add support for differing views & content-type for each template X-Git-Tag: v0.14~31 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=12c85b56f132068657a45a0acb778355653b8fd4;p=catagits%2FCatalyst-View-Email.git add support for differing views & content-type for each template --- diff --git a/lib/Catalyst/View/Email/Template.pm b/lib/Catalyst/View/Email/Template.pm index 87c8b54..5111a99 100644 --- a/lib/Catalyst/View/Email/Template.pm +++ b/lib/Catalyst/View/Email/Template.pm @@ -56,6 +56,21 @@ Sending email is just setting up your stash key, and forwarding to the view. }; $c->forward('View::Email::Template'); +Alternatively if you want more control over your templates you can use the following idiom :- + + templates => [ + { view => 'HTML', + template => 'email/test.html.tt', + content_type => 'text/html' + }, + { view => 'Text', + template => 'email/test.plain.tt', + content_type => 'text/plain' + } + + ] + + If it fails $c->error will have the error message. =cut @@ -87,16 +102,16 @@ sub process { croak "No template specified for rendering" unless $c->stash->{$stash_key}->{template} or $c->stash->{$stash_key}->{templates}; - # Where to look my $template_prefix = $self->config->{template_prefix}; my @templates = (); - if ( $c->stash->{$stash_key}->{templates} ) { + + if ( $c->stash->{$stash_key}->{templates} && !ref $c->stash->{$stash_key}->{templates}[0]) { push @templates, map { join('/', $template_prefix, $_); } @{$c->stash->{$stash_key}->{templates}}; - } else { + } elsif($c->stash->{$stash_key}->{template}) { push @templates, join('/', $template_prefix, $c->stash->{$stash_key}->{template}); } @@ -135,6 +150,34 @@ sub process { body => $output ); } + + #add user parts :- + if ( $c->stash->{$stash_key}->{'templates'} && ref $c->stash->{$stash_key}->{templates}[0] ) { + foreach my $part (@{$c->stash->{$stash_key}->{'templates'}}) { + my $view = $c->view($part->{'view'} || $self->config->{default_view}); + + my $content_type = $part->{'content_type'} || 'text/plain'; + unless ( $view->can('render') ) { + croak "Part does not have valid render view"; + } + + my $output = $view->render( $c, $part->{'template'}, { + 'content_type' => $content_type, + %{$c->stash} }); + + if ( ref $output ) { + croak $output->can("as_string") ? $output->as_string : $output; + } + + push @parts, Email::MIME->create( + attributes => { + content_type => $content_type + }, + body => $output + ); + } + } + delete $c->stash->{email}->{body}; $c->stash->{email}->{parts} ||= []; push @{$c->stash->{email}->{parts}}, @parts; @@ -172,6 +215,8 @@ something along these lines: J. Shirley +Simon Elliott + =head1 LICENSE This library is free software, you can redistribute it and/or modify it under