<?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>Phil Brown&#039;s Web Development Blog &#187; layout</title>
	<atom:link href="http://blog.philipbrown.id.au/tag/layout/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.philipbrown.id.au</link>
	<description></description>
	<lastBuildDate>Mon, 24 May 2010 05:11:00 +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>Changing layouts with Zend ContextSwitch</title>
		<link>http://blog.philipbrown.id.au/2009/02/changing-layouts-with-zend-contextswitch/</link>
		<comments>http://blog.philipbrown.id.au/2009/02/changing-layouts-with-zend-contextswitch/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 03:19:58 +0000</pubDate>
		<dc:creator>Phil</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Context Switch]]></category>
		<category><![CDATA[layout]]></category>

		<guid isPermaLink="false">http://morecowbell.net.au/?p=51</guid>
		<description><![CDATA[Ever since discovering Zend Framework&#8217;s ContextSwitch and AjaxContext action helpers, I&#8217;ve been hooked. What&#8217;s not to love about writing one controller action and having the request dictate the response format.
Whilst this generally does the job for XML, JSON and AJAX HTML snippets where any layout is disabled, sometimes you just need to wrap that content [...]]]></description>
			<content:encoded><![CDATA[<p>Ever since discovering Zend Framework&#8217;s <a href="http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.contextswitch">ContextSwitch and AjaxContext</a> action helpers, I&#8217;ve been hooked. What&#8217;s not to love about writing one controller action and having the request dictate the response format.</p>
<p>Whilst this generally does the job for XML, JSON and AJAX HTML snippets where any layout is disabled, sometimes you just need to wrap that content in <em>something</em>.</p>
<p>I often find myself writing small configuration screens that work nicely in modal style dialogs as opposed to a devoted web page. Generally I&#8217;ll render these screens as inline frames using my favourite <a href="http://jquery.com/">JavaScript library</a> and <a href="http://swip.codylindley.com/DOMWindowDemo.html">DOM Window</a> (a <a href="http://jquery.com/demo/thickbox/">ThickBox</a> successor). The problem with iframes is they require a complete HTML document. ContextSwitch just wasn&#8217;t cutting it as I needed some kind of layout but obviously not the full blown, menus and all default.</p>
<p>The solution was to implement my own custom context, complete with a post-processing callback function to alter the layout script. Here&#8217;s how I&#8217;ve done it.</p>
<p>First, I added a static method to my Bootstrap class to add a suffix to the layout script name should a context be present.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> setLayoutContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$layout</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;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">!==</span> <span style="color: #000088;">$layout</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$layout</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$context</span> <span style="color: #339933;">=</span> Zend_Controller_Action_HelperBroker<span style="color: #339933;">::</span><span style="color: #004000;">getStaticHelper</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ContextSwitch'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCurrentContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">!==</span> <span style="color: #000088;">$context</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$layout</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setLayout</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$layout</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLayout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$context</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>For example, given the default layout script <code>layout.phtml</code> and context <code>iframe</code>, this method will set the layout script to <code>layout.iframe.phtml</code></p>
<p>Now I just need to add the context to the action helper. Again, in my Bootstrap class.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> setupActionHelpers<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// This method is called during application bootstrapping</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Add &quot;iframe&quot; context</span>
    <span style="color: #000088;">$iframeContext</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'suffix'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'iframe'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'callbacks'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'post'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'setLayoutContext'</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$contextSwitch</span> <span style="color: #339933;">=</span> Zend_Controller_Action_HelperBroker<span style="color: #339933;">::</span><span style="color: #004000;">getStaticHelper</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ContextSwitch'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$contextSwitch</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addContext</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'iframe'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$iframeContext</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Ok, that&#8217;s all the preparation work out of the way. To add this context to a controller action, you simply, well, add it.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MyController <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Action
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_helper<span style="color: #339933;">-&gt;</span><span style="color: #004000;">contextSwitch</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addActionContext</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'config'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'iframe'</span><span style="color: #009900;">&#41;</span>
                                     <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAutoDisableLayout</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
                                     <span style="color: #339933;">-&gt;</span><span style="color: #004000;">initContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> configAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Controller tasks here</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>All that&#8217;s left to do is ensure your links to the context enabled controller action include the correct <code>format</code> parameter, for example <code>/my/config/format/iframe</code> or <code>/my/config?format=iframe</code>.</p>
<p>One thing I&#8217;ve noticed is that the ContextSwitch helper should be configured <em>after</em> your view and layout are initialised.</p>
<p>Thanks to Jeff Carouth for the <a href="http://carouth.com/blog/focus/context-switching-layouts-with-the-zend-framework--26">inspiration</a> for this post.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.philipbrown.id.au/2009/02/changing-layouts-with-zend-contextswitch/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->