[Catalyst-commits] r6626 - trunk/Catalyst-View-Email/lib/Catalyst/View/Email

purge at dev.catalyst.perl.org purge at dev.catalyst.perl.org
Wed Aug 8 18:30:04 GMT 2007


Author: purge
Date: 2007-08-08 18:30:04 +0100 (Wed, 08 Aug 2007)
New Revision: 6626

Modified:
   trunk/Catalyst-View-Email/lib/Catalyst/View/Email/Template.pm
Log:
add support for differing views & content-type for each template

Modified: trunk/Catalyst-View-Email/lib/Catalyst/View/Email/Template.pm
===================================================================
--- trunk/Catalyst-View-Email/lib/Catalyst/View/Email/Template.pm	2007-08-08 00:39:14 UTC (rev 6625)
+++ trunk/Catalyst-View-Email/lib/Catalyst/View/Email/Template.pm	2007-08-08 17:30:04 UTC (rev 6626)
@@ -56,6 +56,21 @@
     };
     $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 @@
     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 @@
             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 @@
 
 J. Shirley <jshirley at gmail.com>
 
+Simon Elliott <cpan at browsing.co.uk>
+
 =head1 LICENSE
 
 This library is free software, you can redistribute it and/or modify it under




More information about the Catalyst-commits mailing list