<?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>vandenbos.org &#187; zend_layout</title>
	<atom:link href="http://blog.vandenbos.org/tag/zend_layout/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.vandenbos.org</link>
	<description>Matthijs van den Bos&#039; thoughts on web development topics</description>
	<lastBuildDate>Mon, 08 Feb 2010 08:11:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Zend Framework: module specific layout</title>
		<link>http://blog.vandenbos.org/2009/07/19/zend-framework-module-specific-layout/</link>
		<comments>http://blog.vandenbos.org/2009/07/19/zend-framework-module-specific-layout/#comments</comments>
		<pubDate>Sun, 19 Jul 2009 14:30:00 +0000</pubDate>
		<dc:creator>Matthijs</dc:creator>
				<category><![CDATA[zend framework]]></category>
		<category><![CDATA[bootstrap]]></category>
		<category><![CDATA[layout]]></category>
		<category><![CDATA[module]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[Zend_Application]]></category>
		<category><![CDATA[zend_layout]]></category>

		<guid isPermaLink="false">http://blog.vandenbos.org/?p=130</guid>
		<description><![CDATA[In this article I will show you how set up module specific layouts. Many people find it confusing that module resources like layouts are always loaded, regardless of the module requested. The reason for this is that at the time of bootstrap, the request is not yet determined and the application does not know what [...]]]></description>
			<content:encoded><![CDATA[<p>In this article I will show you how set up module specific layouts. Many people find it confusing that module resources like layouts are always loaded, regardless of the module requested. The reason for this is that at the time of bootstrap, the request is not yet determined and the application does not know what module was requested. To set up resources like layouts, databases, translations etc. for specific modules ONLY when that module is requested, your best bet is a front controller plugin or action helper.</p>
<p><span id="more-130"></span></p>
<h2>Usage example</h2>
<p>Default layout settings in application.ini:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">resources<span style="color: #339933;">.</span>layout<span style="color: #339933;">.</span>layout <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;default&quot;</span>
resources<span style="color: #339933;">.</span>layout<span style="color: #339933;">.</span>layoutPath <span style="color: #339933;">=</span> APPLICATION_PATH <span style="color: #0000ff;">&quot;/views/layouts&quot;</span></pre></div></div>

<p>Then, if you use my ModuleSetup resource as described in <a href="http://blog.vandenbos.org/2009/07/07/zend-framework-module-config/">module config</a>, you can set the module layout options in the module.ini for that module:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">resources<span style="color: #339933;">.</span>layout<span style="color: #339933;">.</span>layout <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;modulename_default&quot;</span>
&nbsp;
<span style="color: #339933;">;</span> this layoutpath is relative to the directory of the module itself
<span style="color: #339933;">;</span> in this <span style="color: #b1b100;">case</span><span style="color: #339933;">:</span> application<span style="color: #339933;">/</span>modules<span style="color: #339933;">/</span>modulename<span style="color: #339933;">/</span>views<span style="color: #339933;">/</span>layouts
resources<span style="color: #339933;">.</span>layout<span style="color: #339933;">.</span>layoutPath <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;views/layouts&quot;</span></pre></div></div>

<p>or, you can set the module config in your application.ini normally, as described in the Zend Framework reference:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">modulename<span style="color: #339933;">.</span>resources<span style="color: #339933;">.</span>layout<span style="color: #339933;">.</span>layout <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;modulename_default&quot;</span>
&nbsp;
<span style="color: #339933;">;</span> this layoutpath is relative to the directory of the module itself
<span style="color: #339933;">;</span> in this <span style="color: #b1b100;">case</span><span style="color: #339933;">:</span> application<span style="color: #339933;">/</span>modules<span style="color: #339933;">/</span>modulename<span style="color: #339933;">/</span>views<span style="color: #339933;">/</span>layouts
modulename<span style="color: #339933;">.</span>resources<span style="color: #339933;">.</span>layout<span style="color: #339933;">.</span>layoutPath <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;views/layouts&quot;</span></pre></div></div>

<p><em>* note: you can set all options for module layouts sources the same way as for the application layout resource. If you don&#8217;t specify a different layoutPath, the standard path is used</em></p>
<p>The result is that when the module &#8216;modulename&#8217; is requested, the layout &#8216;modulename_default.phtml&#8217; in the directory &#8216;application/modules/modulename/views/layouts&#8217; is used. Otherwise, the main layout is used.</p>
<h2>Code</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Rexus_Controller_Plugin_RequestedModuleLayoutLoader
    <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_plugin_Abstract
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> preDispatch<span style="color: #009900;">&#40;</span>Zend_Controller_Request_Abstract <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$config</span>     <span style="color: #339933;">=</span> Zend_Controller_Front<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'bootstrap'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOptions</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$moduleName</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getModuleName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$moduleName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'resources'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'layout'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'layout'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$layoutScript</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$moduleName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'resources'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'layout'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'layout'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            Zend_Layout<span style="color: #339933;">::</span><span style="color: #004000;">getMvcInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setLayout</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$layoutScript</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$moduleName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'resources'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'layout'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'layoutPath'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$layoutPath</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$moduleName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'resources'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'layout'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'layoutPath'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$moduleDir</span> <span style="color: #339933;">=</span> Zend_Controller_Front<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getModuleDirectory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            Zend_Layout<span style="color: #339933;">::</span><span style="color: #004000;">getMvcInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setLayoutPath</span><span style="color: #009900;">&#40;</span>
                <span style="color: #000088;">$moduleDir</span><span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">DIRECTORY_SEPARATOR</span> <span style="color: #339933;">.</span><span style="color: #000088;">$layoutPath</span>
            <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>To use this, drop the class above in your library and set the following in your application.ini:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">resources<span style="color: #339933;">.</span>frontController<span style="color: #339933;">.</span>plugins<span style="color: #339933;">.</span>layoutloader <span style="color: #339933;">=</span> Rexus_Controller_Plugin_RequestedModuleLayoutLoader</pre></div></div>

<p>If you want to know more about handling other types of resources with modules, check my post about <a href="http://blog.vandenbos.org/2009/07/09/zend-framework-per-module-translation-sources/">module translation sources</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.vandenbos.org/2009/07/19/zend-framework-module-specific-layout/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
