<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>TK Studios &#187; zend framework</title>
	<atom:link href="http://www.tkstudios.com/tag/zend-framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tkstudios.com</link>
	<description>Web Application Design &#38; Development</description>
	<lastBuildDate>Mon, 20 Jun 2011 16:07:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Zend Framework: Email templating with layouts &amp; views</title>
		<link>http://www.tkstudios.com/2009/12/02/zend-framework-email-templating-with-layouts-views/</link>
		<comments>http://www.tkstudios.com/2009/12/02/zend-framework-email-templating-with-layouts-views/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 16:14:24 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Listy]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://www.tkstudios.com/?p=99</guid>
		<description><![CDATA[This is a solution that I came up with for sending out welcome emails, comment notifications, etc, with nice standardized HTML &#38; text layouts, using a few components of the Zend Framework. Basically you create a view (here called $renderer) and a layout ($layout), then render your views and layouts for both HTML and text [...]]]></description>
			<content:encoded><![CDATA[<p>This is a solution that I came up with for sending out welcome emails, comment notifications, etc, with nice standardized HTML &amp; text layouts, using a few components of the Zend Framework.</p>
<p>Basically you create a view (here called $renderer) and a layout ($layout), then render your views and layouts for both HTML and text versions. My file tree looks like this:</p>
<pre>application/
	emails/
		welcome.html.phtml
		welcome.text.phtml
		layouts/
			layout.html.phtml
			layout.text.phtml</pre>
<p>So, &#8216;welcome&#8217; would be your template name, with html and text versions. The layout is what you want wrapped around either version of every email you send out (header graphics, font settings, contact info, etc).</p>
<p><span id="more-99"></span></p>
<p>I keep all of my mail functions in one class, creating a new function for each email that needs to be sent out regularly in an automatic fashion. You could even use this for newsletters, but I prefer more robust environments like <a href="http://www.mailchimp.com/">MailChimp</a> or <a href="http://www.campaignmonitor.com/">CampaignMonitor</a> for that sort of thing.</p>
<p>This simple function handles all the rendering, file paths, etc. It just needs to be called with the proper template name, and an array of items to be passed to the view renderer.</p>
<p>Here&#8217;s the meat of it:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * getMailContent
 * Renders mailer content in text and html and passes it back as an array('html'=&amp;gt;content,'text'=&amp;gt;content)
 *
 * @param string $templatename the name of the email template to use (APP/emails/[name].[format].phtml)
 * @param array $substitutions key=&amp;gt;value array of variables to pass to the view renderers
 * @return array
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getMailContent<span style="color: #009900;">&#40;</span><span style="color: #000088;">$templatename</span><span style="color: #339933;">,</span> <span style="color: #000088;">$substitutions</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// create a view renderer and set it to app_path/emails/</span>
    <span style="color: #000088;">$renderer</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_View<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$renderer</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>setScriptPath<span style="color: #009900;">&#40;</span><span style="color: #990000;">realpath</span><span style="color: #009900;">&#40;</span>APPLICATION_PATH <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/emails/'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// create a layout object and set it to app_path/emails/layouts</span>
    <span style="color: #000088;">$layout</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Layout<span style="color: #009900;">&#40;</span>APPLICATION_PATH <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/emails/layouts/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$layout</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>setView<span style="color: #009900;">&#40;</span><span style="color: #000088;">$renderer</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// this probably isn't even necessary</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// assign all substitutions (e.g. view variables) to the view renderer</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$substitutions</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #000088;">$sub</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$renderer</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #000088;">$key</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$sub</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// output array        </span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// render text version of template &amp;amp; assign to output['text']</span>
    <span style="color: #000088;">$layout</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>content <span style="color: #339933;">=</span> <span style="color: #000088;">$renderer</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>render<span style="color: #009900;">&#40;</span><span style="color: #000088;">$templatename</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.text.phtml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$layout</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>setLayout<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'layout.text'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$output</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'text'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$layout</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>render<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// render html version of template &amp;amp; assign to output['html']</span>
    <span style="color: #000088;">$layout</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>content <span style="color: #339933;">=</span> <span style="color: #000088;">$renderer</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>render<span style="color: #009900;">&#40;</span><span style="color: #000088;">$templatename</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.html.phtml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$layout</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>setLayout<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'layout.html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$output</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'html'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$layout</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>render<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// all done, return output</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$output</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Nice and simple, huh? All you need to do is call it and then assign the output to your Zend_Mail object like so:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">        <span style="color: #000088;">$bodies</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getMailContent<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'new_comment'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'comment'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #000088;">$comment</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'comment_list'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #000088;">$comment_list</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'comment_user'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #000088;">$comment_user</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'list_owner'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #000088;">$list_owner</span>
        <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$mail</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>setBodyHtml<span style="color: #009900;">&#40;</span><span style="color: #000088;">$bodies</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'html'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$mail</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>setBodyText<span style="color: #009900;">&#40;</span><span style="color: #000088;">$bodies</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'text'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Your email template accesses the variables passed to it just as it would a normal view script:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #009900;">&#91;</span>variable<span style="color: #009900;">&#93;</span></pre></div></div>

<p>Commentary / suggestions / etc welcome in comments.</p>
<div class="tweetthis" style="text-align:left;"><p> <a class="tt" href="http://twitter.com/intent/tweet?text=Zend+Framework%3A+Email+templating+with+layouts+%26+views+http%3A%2F%2Fis.gd%2FR6j2qY+%28%40tkstudios%29" title="Post to Twitter"><img class="nothumb" src="http://www.tkstudios.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a class="tt" href="http://twitter.com/intent/tweet?text=Zend+Framework%3A+Email+templating+with+layouts+%26+views+http%3A%2F%2Fis.gd%2FR6j2qY+%28%40tkstudios%29" title="Post to Twitter">Tweet This Post</a> <a class="tt" href="http://digg.com/submit?url=http://www.tkstudios.com/2009/12/02/zend-framework-email-templating-with-layouts-views/&amp;title=Zend+Framework%3A+Email+templating+with+layouts+%26+views" title="Post to Digg"><img class="nothumb" src="http://www.tkstudios.com/wp-content/plugins/tweet-this/icons/en/digg/tt-digg.png" alt="Post to Digg" /></a> <a class="tt" href="http://digg.com/submit?url=http://www.tkstudios.com/2009/12/02/zend-framework-email-templating-with-layouts-views/&amp;title=Zend+Framework%3A+Email+templating+with+layouts+%26+views" title="Post to Digg">Digg This Post</a> <a class="tt" href="http://reddit.com/submit?url=http://www.tkstudios.com/2009/12/02/zend-framework-email-templating-with-layouts-views/&amp;title=Zend+Framework%3A+Email+templating+with+layouts+%26+views" title="Post to Reddit"><img class="nothumb" src="http://www.tkstudios.com/wp-content/plugins/tweet-this/icons/en/reddit/tt-reddit.png" alt="Post to Reddit" /></a> <a class="tt" href="http://reddit.com/submit?url=http://www.tkstudios.com/2009/12/02/zend-framework-email-templating-with-layouts-views/&amp;title=Zend+Framework%3A+Email+templating+with+layouts+%26+views" title="Post to Reddit">Post to Reddit</a> <a class="tt" href="http://stumbleupon.com/submit?url=http://www.tkstudios.com/2009/12/02/zend-framework-email-templating-with-layouts-views/&amp;title=Zend+Framework%3A+Email+templating+with+layouts+%26+views" title="Post to StumbleUpon"><img class="nothumb" src="http://www.tkstudios.com/wp-content/plugins/tweet-this/icons/en/su/tt-su.png" alt="Post to StumbleUpon" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://www.tkstudios.com/2009/12/02/zend-framework-email-templating-with-layouts-views/&amp;title=Zend+Framework%3A+Email+templating+with+layouts+%26+views" title="Post to StumbleUpon">Stumble This Post</a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.tkstudios.com/2009/12/02/zend-framework-email-templating-with-layouts-views/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

