<?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>djsipe.com &#187; mvc</title>
	<atom:link href="http://blog.djsipe.com/tag/mvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.djsipe.com</link>
	<description>Web Development</description>
	<lastBuildDate>Sat, 31 Dec 2011 16:25:34 +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>Using a Single Template with Zend Framework&#8217;s ViewRenderer</title>
		<link>http://blog.djsipe.com/2008/02/16/using-a-single-template-with-zend-frameworks-viewrenderer/</link>
		<comments>http://blog.djsipe.com/2008/02/16/using-a-single-template-with-zend-frameworks-viewrenderer/#comments</comments>
		<pubDate>Sat, 16 Feb 2008 22:57:20 +0000</pubDate>
		<dc:creator>DJ</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://www.djsipe.com/2008/02/16/using-a-single-template-with-zend-frameworks-viewrenderer/</guid>
		<description><![CDATA[Update: With the release of Zend_Layout, the need to this sort of workaround has diminished. It&#8217;s no secret that I&#8217;m a big fan of the Zend Framework. After fumbling around with a primitive MVC application at work, I was instantly won over by the clean, and intuitive way they implement MVC. Round about version 0.9, [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update: </strong>With the release of <a href="http://framework.zend.com/manual/en/zend.layout.html" title="Zend_Layout" target="_blank">Zend_Layout</a>, the need to this sort of workaround has diminished.</p>
<p>It&#8217;s no secret that I&#8217;m a big fan of the <a href="http://framework.zend.com/" target="_blank">Zend Framework</a>.  After fumbling around with a primitive <a href="http://en.wikipedia.org/wiki/Model-view-controller" target="_blank">MVC</a> application at work, I was instantly won over by the clean, and intuitive way they implement MVC.  Round about version 0.9, the <a href="http://devzone.zend.com/article/2072-Zend-Frameworks-MVC-Introduces-the-ViewRenderer" target="_blank">ViewRenderer</a> was introduced.  ViewRenderer is, I&#8217;d say, 90% useful.  It makes certain assumptions about the way you&#8217;re using the framework.  If you color between the lines it&#8217;s a great help.</p>
<p>One thing I almost always have to do is override the way it selects and automatically renders templates based on controller and action names.  I typically roll with a single template and load elements into that template.  Of course, I could just disable ViewRenderer altogether, but it does do a lot of other things that I find helpful, like initializing the <a href="http://framework.zend.com/manual/en/zend.view.html" target="_blank">View</a>.</p>
<p>My solution: extend the default Zend_Controller_Action class.  There&#8217;s only a few things to do:</p>
<ol>
<li>Disable the auto-render functionality of ViewRenderer</li>
<li>Add a new <code>$_template</code> property to hold the template to render</li>
<li>Render a single template in the <code>postDispatch</code> method</li>
</ol>
<p>Here&#8217;s the code:</p>
<pre class="brush: php; title: ; notranslate">
require_once &quot;Zend/Controller/Action.php&quot;;

class Blink_Controller_Action extends Zend_Controller_Action
{
    protected $_template = &quot;defaultTemplate.phtml&quot;;

    public function init()
    {
        $this-&gt;_helper-&gt;viewRenderer-&gt;setNoRender(true);
        parent::init();
    }

    public function postDispatch()
    {
        echo $this-&gt;view-&gt;render($this-&gt;_template);
    }
}
</pre>
<p>With our new controller action, we can then largely forget about rendering the View.  All we have to do is assign values to the View:</p>
<pre class="brush: php; title: ; notranslate">
require_once &quot;Blink/Controller/Action.php&quot;;

class MyController extends Blink_Controller_Action
{
    public function IndexAction ()
    {
        $this-&gt;view-&gt;myValue = &quot;some value&quot;;
        // Assign more values...
        // Do business logic...

        // Optionally, select a new template to render
        $this-&gt;_template = &quot;differentTemplate.phtml&quot;;
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.djsipe.com/2008/02/16/using-a-single-template-with-zend-frameworks-viewrenderer/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

