<?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; Zend_View_Helper_Abstract</title>
	<atom:link href="http://www.evilprofessor.co.uk/tag/zend_view_helper_abstract/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>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>
	</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! -->
