<?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>Evilprofessor.co.uk &#187; view</title>
	<atom:link href="http://www.evilprofessor.co.uk/tag/view/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.evilprofessor.co.uk</link>
	<description>The website of Steven Lloyd Watkin</description>
	<lastBuildDate>Thu, 04 Aug 2011 09:56:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Zend Framework: Render If Exists</title>
		<link>http://www.evilprofessor.co.uk/349-zend-framework-render-if-exists/</link>
		<comments>http://www.evilprofessor.co.uk/349-zend-framework-render-if-exists/#comments</comments>
		<pubDate>Sun, 12 Dec 2010 16:19:58 +0000</pubDate>
		<dc:creator>Steven Lloyd Watkin</dc:creator>
				<category><![CDATA[Computing]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Zend_View]]></category>
		<category><![CDATA[Zend_View_Helper]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[render]]></category>
		<category><![CDATA[view]]></category>
		<category><![CDATA[view helper]]></category>
		<category><![CDATA[zend]]></category>
		<category><![CDATA[zend_view]]></category>
		<category><![CDATA[Zend_View_Helper_Abstract]]></category>

		<guid isPermaLink="false">http://www.evilprofessor.co.uk/?p=349</guid>
		<description><![CDATA[Summary This is a quick post to discuss the rather simple view helper I created for rendering a Zend Framework style view file only if it exists. Generally asking the code to render a file which doesn&#8217;t exist will throw an exception. Therefore I created a wrapper for the Zend_View::render() method which determined whether the [...]]]></description>
			<content:encoded><![CDATA[<h2>Summary</h2>
<p>This is a quick post to discuss the rather simple view helper I created for rendering a <a href="http://framework.zend.com" class="kblinker" target="_blank" title="More about Zend Framework &raquo;">Zend Framework</a> style view file only if it exists. Generally asking the code to render a file which doesn&#8217;t exist will throw an exception. Therefore I created a wrapper for the Zend_View::render() method which determined whether the file exists and if so renders, otherwise simply returns an empty string.<br />
<span id="more-349"></span><br />
<div class="wp-caption alignright" style="width: 250px"><img title="Wood" src="http://farm5.static.flickr.com/4114/4829390369_ed85ecc900_m.jpg" alt="" width="240" height="160" /><p class="wp-caption-text">Joost J. Bakker IJmuiden - http://www.flickr.com/photos/joost-ijmuiden/</p></div></p>
<h2>Motivation</h2>
<p>The motivation for this is that I have one controller/view/etc which shows slightly different output depending on a parameter. Some of these outputs have a related help information box defined, some do not. I didn&#8217;t want to have empty files hanging around my code just as much as I didn&#8217;t want to have try {} catch {} blocks in my view file. Therefore, I quickly created a &#8216;render if exists&#8217; view helper for use in my code.</p>
<p>I realise that here I could have just wrapped the render method within a try{}catch{} within the view helper itself. In this case however any exceptions thrown from the rendered view would not bubble back up in the case of something going wrong.</p>
<h2>How It Works</h2>
<p>The view helper is created by extending Zend_View_Helper_Abstract as normally, this injected with the view upon use. The view helper is passed the view file you wish to render:</p>
<pre class="php" name="code">$this-&gt;renderIfExists('info-box.phtml');</pre>
<p>Internally the object gathers the script paths from the view object and loops over them trying to find the file:</p>
<pre class="php" name="code">
    /**
     * Check to see if a <a href="http://www.evilprofessor.co.uk/269-naked-zend_layout-and-zend_view/" class="kblinker" title="More about view script &raquo;">view script</a> exists
     *
     * @return boolean
     */
    protected function _fileExists()
    {
        $paths = $this-&gt;view-&gt;getScriptPaths();
        foreach ($paths as $path) {
            if (file_exists($path . $this-&gt;_file)) {
                return true;
            }
        }
        return false;
    }</pre>
<p>If a matching file is found it uses the standard Zend_View::render() method, otherwise it returns an empty string.</p>
<h2>Summary</h2>
<p>This is a very simple view helper to render a view file if it exists and simply return an empty string if not. It does not use a try{} catch{} block which would prevent any deeper exceptions being thrown from bubbling up and making themselves known. Instead it loops through the script paths defined within the view object and attempts to detect the file before trying to render it.</p>
<p>The full file can be found at my <a title="RenderIfExists View Helper @ GitHub" href="https://github.com/lloydwatkin/Demos/blob/master/zendframework/renderifexists/RenderIfExists.php" target="_blank">github repo</a>.</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.evilprofessor.co.uk%2F349-zend-framework-render-if-exists%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>]]></content:encoded>
			<wfw:commentRss>http://www.evilprofessor.co.uk/349-zend-framework-render-if-exists/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Zend Framework Body Tag View Helper</title>
		<link>http://www.evilprofessor.co.uk/311-zend-framework-body-tag-view-helper/</link>
		<comments>http://www.evilprofessor.co.uk/311-zend-framework-body-tag-view-helper/#comments</comments>
		<pubDate>Sat, 21 Aug 2010 23:13:28 +0000</pubDate>
		<dc:creator>Steven Lloyd Watkin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Zend_Registry]]></category>
		<category><![CDATA[Zend_View_Helper]]></category>
		<category><![CDATA[Body]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[helper]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[view]]></category>
		<category><![CDATA[view helper]]></category>
		<category><![CDATA[zend]]></category>
		<category><![CDATA[Zend_View_Helper_Placeholder_Container_Standalone]]></category>

		<guid isPermaLink="false">http://www.evilprofessor.co.uk/?p=311</guid>
		<description><![CDATA[Summary Here I discuss the creation of a view helper for modifying HTML tags, and more specifically body tags. The created view helper allows functionality similar to the head*/inlineScript view helpers already in the standard Zend Framework view helpers, but allows the programmatic modification of tag attributes. Definitely check out the demo page and the [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_320" class="wp-caption alignright" style="width: 222px"><a href="http://www.evilprofessor.co.uk/wp-content/uploads/2010/08/422213306_02bcf3f7d2_m.jpg"><img class="size-full wp-image-320 " title="Photo from  http://www.flickr.com/photos/daniello/" src="http://www.evilprofessor.co.uk/wp-content/uploads/2010/08/422213306_02bcf3f7d2_m.jpg" alt="Photo from  http://www.flickr.com/photos/daniello/" width="212" height="300" /></a><p class="wp-caption-text">Photo from http://www.flickr.com/photos/daniello/</p></div>
<h2>Summary</h2>
<p>Here I discuss the creation of a view helper for modifying HTML tags, and more specifically <a href="http://www.evilprofessor.co.uk/311-zend-framework-body-tag-view-helper/" class="kblinker" title="More about body tag &raquo;">body tags</a>. The created view helper allows functionality similar to the head*/inlineScript view helpers already in the standard <a href="http://framework.zend.com" class="kblinker" target="_blank" title="More about Zend Framework &raquo;">Zend Framework</a> view helpers, but allows the programmatic modification of tag attributes. Definitely check out the demo page and the code on github.</p>
<h2>Motivation</h2>
<p>The standard Zend Framework view helpers are a great set of tools for streamlining mundane view tasks and allowing for the modification/addition of scripts and header blocks (generally held in the layout) from within the view without applying ugly hacks (i.e. the head*/inlineScript view helpers).</p>
<p>Upon occasion I have found need to make modifications to the &lt;body&gt; tag, for example adding an <em><a title="body onload attribute" href="http://www.w3schools.com/jsref/event_body_onload.asp" target="_blank">onload</a>, class, </em>or <em>style </em>attribute etc. I also required to be able to perform this from within other view helpers. Take this following contrived example&#8230;</p>
<blockquote><p>On website X, certain pages include standard <a href="http://www.dojotoolkit.org/" class="kblinker" target="_blank" title="More about Dojo &raquo;">dojo</a> forms. These dojo forms are held within view helpers for convenience. Generally it has been decided not to include the dojo <a href="http://www.w3schools.com/css/css_intro.asp" class="kblinker" target="_blank" title="More about CSS &raquo;">CSS</a> classes in the body tag and only add them when necessary. There maybe several view helpers on the page that need to add their own attributes to the body tag.<em> (I said it was contrived)</em></p></blockquote>
<p>The code is available in my GIT repository @ <a title="Body tag view helper @ github" href="http://github.com/lloydwatkin/Demos/tree/master/zendframework/abstracttag/" target="_blank">github</a> and the <a title="Body Tag View Helper Demo" href="http://www.evilprofessor.co.uk/uploads/php/zendframework/abstracttag/demo.php" target="_blank">demo page</a>.<br />
<span id="more-311"></span></p>
<h2>How it works</h2>
<p>When creating the body tag view helper I soon realised that most of the code was not specific to what I was developing so I pulled out the common functionality into an abstract class.  By concreting the  abstract class it is possible to modify any tag in the same way as the body tag discussed here making the code much more versitile.</p>
<p>Underneath the hood (so to speak) the code is based on that of the head* and the inlineScript view helpers. It utilises Zend_View_Helper_Placeholder_Container_Standalone to store the attributes and their values until they are required. The container itself uses <a href="http://framework.zend.com/manual/en/zend.registry.html" class="kblinker" target="_blank" title="More about Zend_Registry &raquo;">Zend_Registry</a> to store values under the key defined in the class (if you look at the <a href="http://www.phpunit.de/" class="kblinker" target="_blank" title="More about unit test &raquo;">unit tests</a> I clear the registry down after each test).</p>
<p>The code would fit in well with the &#8220;<a href="http://www.evilprofessor.co.uk/269-naked-zend_layout-and-zend_view/" class="kblinker" title="More about Naked Zend_Layout and Zend_View &raquo;">Naked Zend_Layout and Zend_View</a>&#8221; code I wrote about a couple of weeks ago or as part of the standard Zend Framework <a href="http://en.wikipedia.org/wiki/Model%E2%80%93View%E2%80%93Controller" class="kblinker" target="_blank" title="More about MVC &raquo;">MVC</a> install.</p>
<p><strong>Usage</strong></p>
<p>The class should be very simple to use. Firstly two entries are required within your layout script as follows:</p>
<pre class="php" name="code" type="code">&lt;?php echo $this-&gt;bodyTag() ?&gt;
&lt;?php echo $this-&gt;bodyTag()-&gt;toString(Pro_View_Helper_AbstractTag::CLOSE) ?&gt;</pre>
<p>Note: You&#8217;ll need to change the class name should you move the view helper to your own library. Obviously the constant is available through the implementation (bodyTag) as well as through the abstract class.</p>
<p>An example of making changes to your body tag using the view helper to echo &lt;body class=&#8221;nihilo myclass&#8221; style=&#8221;text-align-left; width: 80em&#8221;&gt; is:</p>
<pre class="php" name="code" type="code">&lt;?php
$this-&gt;bodyTag('class', 'nihilo')
    -&gt;bodyTag('style', 'text-align: left')
    -&gt;bodyTag('style', 'width: 80em', false, ';')
    -&gt;bodyTag('class', 'myclass')
?&gt;</pre>
<p>This can be done throughout the view file, in several view helpers, and in the layout file (provided its before the body tag is written out). Attributes can be removed/overwritten whilst values can be added and appended (separator can be passed). If the same attribute value is passed several times only a single copy is written with the attribute.</p>
<p>For a full demo either fork the github repository and run the demo.php file in your own browser, or alternatively visit <a title="Body tag view helper demo" href="http://www.evilprofessor.co.uk/uploads/php/zendframework/abstracttag/demo.php" target="_blank">this page</a> to see it in action. Usage of the <a href="http://github.com/lloydwatkin/Demos/blob/master/zendframework/abstracttag/demo.php" target="_blank">demo.php</a> script assumes you have the Zend Autoloader (or similar) already setup.</p>
<h2>Creating your own Tag helper</h2>
<p>To implement your own view helper simply extend the abstract tag class and overwrite the following class properties:</p>
<ul>
<li><em>$_regKey</em>: A unique key for the tag (used for Zend_Registry)</li>
<li><em>$_validAttributes</em>: An array of valid attribute names, should be lowercase to comply with <a href="http://www.w3.org/" class="kblinker" target="_blank" title="More about w3c &raquo;">W3C</a></li>
<li><em>$_selfClosing</em>: Boolean value to specify if tag is self closing or not, e.g. <strong>&lt;br/&gt;</strong></li>
<li><em>$_tagName</em>: Name of the tag (i.e. <strong>body</strong> in this example)</li>
</ul>
<p>So, finally the code for the bodyTag view helper itself, its very compact:</p>
<pre class="php" name="code" type="code">/**
 * View helper for the body tag
 *
 * @author     Lloyd Watkin
 * @since      21/08/2010
 * @package    Pro
 * @subpackage ViewHelper
 */
class Pro_View_Helper_BodyTag
    extends Pro_View_Helper_AbstractTag
{
    /**
     * Registry key for placeholder
     *
     * @var string
     */
    protected $_regKey = 'Pro_View_Helper_BodyTag';

    /**
     * Which attributes are valid
     *
     * Currently only STF attributes supported
     * (S = STRICT, T = TRANSITIONAL, F = FRAMESET)
     *
     * @see http://www.w3schools.com/tags/tag_body.asp
     * @var array
     */
    protected $_validAttributes = array(
        /* Standard Attributes */
        'class', 'dir', 'id', 'lang', 'style', 'title', '<a href="http://en.wikipedia.org/wiki/XML" class="kblinker" target="_blank" title="More about XML &raquo;">xml</a>:lang',
        /* Event Attributes */
        'onclick', 'ondblclick', 'onload', 'onmousedown', 'onmousemove',
        'onmouseout', 'onmouseover', 'onmouseup', 'onkeydown', 'onkeypress',
        'onkeyup', 'onunload',
    );

    /**
     * Self closing tag?
     *
     * @var boolean
     */
    protected $_selfClosing = false;    

    /**
     * Tag name
     *
     * @var string
     */
    protected $_tagName = 'body';
}</pre>
<p>If you take a look at the repository there&#8217;s also a group of unit tests that fully cover the functionality. The unit tests are written in <a href="http://www.phpunit.de/" class="kblinker" target="_blank" title="More about phpunit &raquo;">PHPUnit</a> and are in the <a href="http://github.com/lloydwatkin/Demos/blob/master/zendframework/abstracttag/BodyTagTest.php" target="_blank">BodyTagTest.php</a> file.</p>
<h2>Finally&#8230;</h2>
<p>Here I&#8217;ve shown you my implementation of an abstract tag abstract view helper (eek) and its body tag implementation. Whilst the javaScript functionality can be implemented in other ways (other than hard coding at output time) other things can&#8217;t (without javaScript that is) without changing other parts of the application. I hope you find the classes helpful in your application&#8230;</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.evilprofessor.co.uk%2F311-zend-framework-body-tag-view-helper%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>]]></content:encoded>
			<wfw:commentRss>http://www.evilprofessor.co.uk/311-zend-framework-body-tag-view-helper/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Creating URL in Zend Custom View Helper</title>
		<link>http://www.evilprofessor.co.uk/239-creating-url-in-zend-custom-view-helper/</link>
		<comments>http://www.evilprofessor.co.uk/239-creating-url-in-zend-custom-view-helper/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 23:01:57 +0000</pubDate>
		<dc:creator>Steven Lloyd Watkin</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Zend_View]]></category>
		<category><![CDATA[Zend_View_Helper]]></category>
		<category><![CDATA[custom]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[front controller]]></category>
		<category><![CDATA[helper]]></category>
		<category><![CDATA[Router]]></category>
		<category><![CDATA[view]]></category>
		<category><![CDATA[zend]]></category>
		<category><![CDATA[Zend_View_Helper_Abstract]]></category>

		<guid isPermaLink="false">http://www.evilprofessor.co.uk/?p=239</guid>
		<description><![CDATA[This may seem simple, but I was banging my head trying to create a URL in a custom view helper in Zend Framework. I have routing setup which gets the module from the sub-domain in use so I couldn&#8217;t use a simple hardcoded URL. Basically but invoking an instance of the front controller its possible [...]]]></description>
			<content:encoded><![CDATA[<p>This may seem simple, but I was banging my head trying to create a URL in a custom view helper in <a href="http://framework.zend.com" class="kblinker" target="_blank" title="More about Zend Framework &raquo;">Zend Framework</a>. I have routing setup which gets the module from the sub-domain in use so I couldn&#8217;t use a simple hardcoded URL.</p>
<p><span id="more-239"></span></p>
<p>Basically but invoking an instance of the front controller its possible to grab the router and assemble a url. Assemble is the function used in the view helper. The URL is built up from an array of module, controller, action, etc, followed by a second parameter of the route to use. The code is as follows:</p>
<pre name="code" class="php">&lt;?php
/**
 * View helper which returns link category URL
 *
 * @author     Lloyd Watkin
 * @since      25/01/2010
 * @package    ViewHelper
 * @subpackage LinksUrl
 */
class Pro_View_Helper_LinksUrl
    extends Zend_View_Helper_Abstract
{
	/**
	 * Returns link category URL
	 *
	 * @param  Doctrine_Record $category
	 * @param  string          $module
	 * @param  string          $controller
	 * @param  string          $action
	 * @return string Url
	 */
    public function linksUrl($category, $module = 'www',
        $controller = 'links', $action = 'index')
    {
    	$router = Zend_Controller_Front::getInstance()-&gt;getRouter();

        return $router-&gt;assemble(array(
            'module'     =&gt; $module,
            'controller' =&gt; $controller,
            'action'     =&gt; $action,
            'category'   =&gt; "{$category-&gt;id}-{$category-&gt;name}",
        ), 'www-index');
    }
}</pre>
<p>Another way to do this is to invoke Zend_View_Helper_Url itself and call the Url method (if you want to use the helper itself). This can be done by using the following code:</p>
<pre class="php" name="code">&lt;?php
/**
 * View helper which returns link category URL
 *
 * @author     Lloyd Watkin
 * @since      25/01/2010
 * @package    ViewHelper
 * @subpackage LinksUrl
 */
class Pro_View_Helper_LinksUrl
    extends Zend_View_Helper_Abstract
{
	/**
	 * Returns link category URL
	 *
	 * @param  Doctrine_Record $category
	 * @param  string          $module
	 * @param  string          $controller
	 * @param  string          $action
	 * @return string Url
	 */
    public function linksUrl($category, $module = 'www',
        $controller = 'links', $action = 'index')
    {
    	$link = new Zend_View_Helper_Url();

        return $link-&gt;url(array(
            'module'     =&gt; $module,
            'controller' =&gt; $controller,
            'action'     =&gt; $action,
            'category'   =&gt; "{$category-&gt;id}-{$slug}",
        ), 'www-index');
    }
}</pre>
<p>Both almost identical. Not a hard thing to do in the framework but can catch you out ;)</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.evilprofessor.co.uk%2F239-creating-url-in-zend-custom-view-helper%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>]]></content:encoded>
			<wfw:commentRss>http://www.evilprofessor.co.uk/239-creating-url-in-zend-custom-view-helper/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Dynamically add pages to Zend_Navigation container at runtime</title>
		<link>http://www.evilprofessor.co.uk/236-dynamically-add-pages-to-zend_navigation-container-at-runtime/</link>
		<comments>http://www.evilprofessor.co.uk/236-dynamically-add-pages-to-zend_navigation-container-at-runtime/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 22:50:19 +0000</pubDate>
		<dc:creator>Steven Lloyd Watkin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[add]]></category>
		<category><![CDATA[bootstrap]]></category>
		<category><![CDATA[container]]></category>
		<category><![CDATA[dynamically]]></category>
		<category><![CDATA[Navigation]]></category>
		<category><![CDATA[pages]]></category>
		<category><![CDATA[sub-page]]></category>
		<category><![CDATA[view]]></category>
		<category><![CDATA[zend]]></category>
		<category><![CDATA[Zend_Navigation]]></category>

		<guid isPermaLink="false">http://www.evilprofessor.co.uk/?p=236</guid>
		<description><![CDATA[In a continuation on my last post about Zend_Navigation, Route requests for sitemap.xml to custom controller/action, this post is about dymnamically adding pages to a Zend_Navigation container at runtime/script execution. Its all well and good specifying your pages in a ini or xml file but at some point you&#8217;re going to have changing pages in [...]]]></description>
			<content:encoded><![CDATA[<p>In a continuation on my last post about Zend_Navigation, <a href="http://www.evilprofessor.co.uk/231-route-requests-for-sitemap-xml-to-custom-controlleraction/">Route requests for sitemap.xml to custom controller/action</a>, this post is about dymnamically adding pages to a Zend_Navigation container at runtime/script execution.</p>
<p>Its all well and good specifying your pages in a <strong>ini</strong> or <strong><a href="http://en.wikipedia.org/wiki/XML" class="kblinker" target="_blank" title="More about XML &raquo;">xml</a></strong> file but at some point you&#8217;re going to have changing pages in your site that you want as part of a menu, sitemap, or to be included in your breadcrumb trail. Therefore what we need to do is add pages to our Zend_Navigation container at runtime. Examples for this would be in adding news items, blog posts, or page comments, etc.</p>
<p><span id="more-236"></span></p>
<p>In this example I&#8217;m going to add some news posts to my statically defined ini config. To get my news post page configurations I&#8217;ve used a class which returns an array in the following format:</p>
<pre class="php">$pagesToAdd = array (
  0 =&gt;
    array (
      'label' =&gt; 'Fake news story #5...',
      'module' =&gt; 'www',
      'route' =&gt; 'www-index',
      'action' =&gt; 'view',
      'controller' =&gt; 'news',
      'params' =&gt; array (
          'id' =&gt; '5-Fake-news-story--5' )
    ),
  1 =&gt;
    array ( /* More page details */ ),
 );</pre>
<p>As you&#8217;ll notice that the function has returned an array in which are contained arrays which make up the config arrays for Zend_Navigation_Page_Mvc. Therefore, by looping over the array new Zend_Navigation pages can be created from the config array. The next thing to do as part of the loop is to to add the pages in the correct position (alternatively pages can be added in bulk by using -&gt;addPages() method).</p>
<p>To do this, locate the page you wish to add the sub-pages to and simply add the pages. In this case I have used the following code to find my page:</p>
<pre class="php">$container-&gt;findOneBy('label', 'Latest News')-&gt;addPage($page);</pre>
<p>My overall navigation initialisation in the bootstrap therefore looks like this:</p>
<pre class="php">    /**
     * used for handling top-level navigation
     *
     * @return Zend_Navigation
     */
    protected function _initNavigation()
    {
        $this-&gt;bootstrap('layout');
        $layout = $this-&gt;getResource('layout');
        $view = $layout-&gt;getView();
        $config = new Zend_Config_Ini(
            APPLICATION_PATH . '/configs/navigation.ini', APPLICATION_ENV);

        $container = new Zend_Navigation($config-&gt;default);
        // Now add the last 25 news reports
        $news  = new News();
        $pages = $news-&gt;getNavigationEntries();
        foreach ($pages AS $page) {
        	$page = new Zend_Navigation_Page_Mvc($page);
        	$container-&gt;findOneBy('label', 'Latest News')-&gt;addPage($page);
        }
        $view-&gt;navigation($container);
    }</pre>
<p>On thing that needs to be added is some form of caching (using <a href="http://framework.zend.com/manual/en/zend.cache.html" class="kblinker" target="_blank" title="More about Zend_Cache &raquo;">Zend_Cache</a> presumably ;)) otherwise this is going to be quite expensive with each page load.</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.evilprofessor.co.uk%2F236-dynamically-add-pages-to-zend_navigation-container-at-runtime%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>]]></content:encoded>
			<wfw:commentRss>http://www.evilprofessor.co.uk/236-dynamically-add-pages-to-zend_navigation-container-at-runtime/feed/</wfw:commentRss>
		<slash:comments>6</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! -->
