<?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>amnuts &#187; zend_view_stream</title>
	<atom:link href="http://blog.amnuts.com/tag/zend_view_stream/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.amnuts.com</link>
	<description>php projects, javascript, and... stuff.</description>
	<lastBuildDate>Fri, 27 Jan 2012 21:15:48 +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>Extend Zend_View_Stream to easily escape view variables</title>
		<link>http://blog.amnuts.com/2010/10/31/easily-escape-view-variables/</link>
		<comments>http://blog.amnuts.com/2010/10/31/easily-escape-view-variables/#comments</comments>
		<pubDate>Sun, 31 Oct 2010 22:14:55 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[zend_view_stream]]></category>

		<guid isPermaLink="false">http://blog.amnuts.com/?p=207</guid>
		<description><![CDATA[Zend_View_Stream is used pretty much when ever you use Zend_View, and I&#8217;ve blogged about how handy it is before.  But as it&#8217;s a class like any other, you can extend it to give added functionality.  One such use is to add automatic escaping to your view variables when you want.  So instead of doing: You [...]]]></description>
			<content:encoded><![CDATA[<p>Zend_View_Stream is used pretty much when ever you use Zend_View, and <a href="http://blog.amnuts.com/2009/03/24/zend-framework-hidden-gems/">I&#8217;ve blogged about how handy it is before</a>.  But as it&#8217;s a class like any other, you can extend it to give added functionality.  One such use is to add automatic escaping to your view variables when you want.  So instead of doing:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php echo $this-&gt;escape($this-&gt;var); ?&gt;
&lt;?= $this-&gt;escape($this-&gt;var); ?&gt;
</pre>
<p>You could simply do:</p>
<pre class="brush: php; title: ; notranslate">&lt;?=~ $this-&gt;var; ?&gt;</pre>
<p>That&#8217;s a lot simpler, isn&#8217;t it?<br />
<span id="more-207"></span><br />
To do this, we need to do two things; 1) extend the stream class and 2) make sure we register it before Zend Framework registers Zend_View_Stream.</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

class My_Stream extends Zend_View_Stream
{
    /**
     * Opens the script file and converts markup.
     */
    public function stream_open($path, $mode, $options, &amp;$opened_path)
    {
        // get the view script source
        $path        = str_replace('zend.view://', '', $path);
        $this-&gt;_data = file_get_contents($path);

        /**
         * If reading the file failed, update our local stat store
         * to reflect the real stat of the file, then return on failure
         */
        if ($this-&gt;_data === false) {
            $this-&gt;_stat = stat($path);
            return false;
        }

        /**
         * Convert &lt;?= ?&gt; to long-form &lt;?php echo ?&gt; and &lt;? ?&gt; to &lt;?php ?&gt;
         *
         */
        $this-&gt;_data = preg_replace(
            '/\&lt;\?\=~ (.*?);? \?&gt;/',
            '&lt;?php echo $this-&gt;escape($1); ?&gt;',
            $this-&gt;_data
        );
        $this-&gt;_data = preg_replace(
            '/\&lt;\?\=/',
            '&lt;?php echo ',
            $this-&gt;_data
        );
        $this-&gt;_data = preg_replace(
            '/&lt;\?(?!xml|php)/s',
            '&lt;?php ',
            $this-&gt;_data
        );

        /**
         * file_get_contents() won't update PHP's stat cache, so we grab a stat
         * of the file to prevent additional reads should the script be
         * requested again, which will make include() happy.
         */
        $this-&gt;_stat = stat($path);

        return true;
    }
}
</pre>
<p>The observant of you will notice that the above is almost exactly the same as Zend_View_Stream::stream_open(), but with this bit of code added to it:</p>
<pre class="brush: php; title: ; notranslate">
        $this-&gt;_data = preg_replace(
            '/\&lt;\?\=~ (.*?);? \?&gt;/',
            '&lt;?php echo $this-&gt;escape($1); ?&gt;',
            $this-&gt;_data
        );
</pre>
<p>So if you don&#8217;t like using ~ to do the escape this is where you&#8217;d change it.</p>
<p>Now all you have to do is register your stream before Zend Framework does.  You could do this in your bootstrap file, your application class, or whereever it makes sense for your applicaiton.  Basically, you&#8217;d just be dropping in a line like this:</p>
<pre class="brush: php; title: ; notranslate">stream_register_wrapper('zend.view', 'My_Stream');</pre>
<p>Hopefully that gives you some ideas on extending the stream wrapper even more!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.amnuts.com/2010/10/31/easily-escape-view-variables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend Framework hidden gems</title>
		<link>http://blog.amnuts.com/2009/03/24/zend-framework-hidden-gems/</link>
		<comments>http://blog.amnuts.com/2009/03/24/zend-framework-hidden-gems/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 17:16:20 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[streams]]></category>
		<category><![CDATA[zend_view]]></category>
		<category><![CDATA[zend_view_stream]]></category>

		<guid isPermaLink="false">http://blog.amnuts.com/?p=133</guid>
		<description><![CDATA[Sometimes you come across hidden little gems in the Zend Framework that save you time, even if that&#8217;s just down to the amount of text you need to type. The Zend_View holds one of these little gems&#8230; Did you know that you can use the short php open tags and echo tag in your view [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes you come across hidden little gems in the Zend Framework that save you time, even if that&#8217;s just down to the amount of text you need to type.  The Zend_View holds one of these little gems&#8230;</p>
<p>Did you know that you can use the short php open tags and echo tag in your view scripts, and you don&#8217;t even need to have this turned on in the php.ini file?</p>
<p>So you can have things like:</p>
<p><code>&lt;? $this->viewHelper(); ?&gt;</code></p>
<p>and:</p>
<p><code>&lt;?= $this->variable; ?&gt;</code></p>
<p>instead of:</p>
<p><code>&lt;?php $this->viewHelper(); ?&gt;</code><br />
<code>&lt;?php echo $this->variable; ?&gt;</code></p>
<p>Might not seem a lot, but when you have a lot of view scripts to write then you can save quite a few key strokes.</p>
<p>It&#8217;s able to do this, even if you have short_tags off (as it should be!) because Zend_View uses a stream to open and seek through the view script &#8211; Zend_View_Stream.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.amnuts.com/2009/03/24/zend-framework-hidden-gems/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

