<?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>worst solution &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/worst-solution/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>Zend_View_Helper_InlineScript Appends Scripts Twice</title>
		<link>/2010/06/30/zend_view_helper_inlinescript-appends-scripts-twice/</link>
		<comments>/2010/06/30/zend_view_helper_inlinescript-appends-scripts-twice/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 19:34:12 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computer science]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Curly bracket programming languages]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[JavaScript programming language]]></category>
		<category><![CDATA[Scripting languages]]></category>
		<category><![CDATA[worst solution]]></category>

		<guid isPermaLink="false">/?p=1736</guid>
		<description><![CDATA[Double Trouble This is a well known problem, which is well described as a bug in Zend Framework&#8217;s issue tracker. Once you add some scripts with the inlineScript helper you&#8217;ll receive them twice in the code. This makes the page slower and leads to some crashes. $scripts->appendFile('my-file1.js') ->appendFile('my-file2.js') ->appendScript('alert("test")'); This will print the alert twice. &#8230; <a href="/2010/06/30/zend_view_helper_inlinescript-appends-scripts-twice/" class="more-link">Continue reading <span class="screen-reader-text">Zend_View_Helper_InlineScript Appends Scripts Twice</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/04/27/inline-scripts-with-zend_view_helper_inlinescript/" rel="bookmark" title="Inline Scripts with Zend_View_Helper_InlineScript">Inline Scripts with Zend_View_Helper_InlineScript </a></li>
<li><a href="/2010/06/24/zend-framework-inject-javascript-code-in-a-actionview/" rel="bookmark" title="Zend Framework: Inject JavaScript Code in a Action/View">Zend Framework: Inject JavaScript Code in a Action/View </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
<li><a href="/2010/05/25/download-files-with-zend-framework/" rel="bookmark" title="Download Files with Zend Framework">Download Files with Zend Framework </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Double Trouble</h2>
<p><a href="/wp-content/uploads/2010/06/duplicate.jpg"><img src="/wp-content/uploads/2010/06/duplicate.jpg" alt="duplicate" title="duplicate" width="430" height="241" class="aligncenter size-full wp-image-1742" srcset="/wp-content/uploads/2010/06/duplicate.jpg 430w, /wp-content/uploads/2010/06/duplicate-300x168.jpg 300w" sizes="(max-width: 430px) 100vw, 430px" /></a><br />
This is a well known problem, which is well described as a bug in Zend Framework&#8217;s issue tracker. Once you add some scripts with the inlineScript helper you&#8217;ll receive them twice in the code. This makes the page slower and leads to some crashes.</p>
<pre lang="php">
$scripts->appendFile('my-file1.js')
        ->appendFile('my-file2.js')
        ->appendScript('alert("test")');
</pre>
<p>This will print the alert twice.</p>
<pre lang="html5strict">
<script src="my-file1.js"></script>
<script src="my-file2.js"></script>
<script>
//<!--
alert("test");
//-->
</script>
<script>
//<!--
alert("test");
//-->
</script>
</pre>
<h2>Quick Solution</h2>
<p>Although this is the worst solution I&#8217;ve ever made &#8211; it works! There is a check for duplicates in the HeadScript helper around line 240:</p>
<pre lang="php">
if (!$this->_isDuplicate($content)) {
...
</pre>
<p>and then a check in the _isDuplicate method(), ~ line 270:</p>
<pre lang="php">
if (($item->source === null)
    && array_key_exists('src', $item->attributes)
    && ($file == $item->attributes['src']))
{
    return true;
}
</pre>
<p>The thing you&#8217;ve to do is to add such a check for the appendScript() and not only for appendFile() method &#8211; again ~ line 230:</p>
<pre lang="php">
case 'script':
	if (!$this->_isDuplicate($content)) {
	    $item = $this->createData($type, $attrs, $content);
	    if ('offsetSet' == $action) {
	        $this->offsetSet($index, $item);
	    } else {
	        $this->$action($item);
	    }
	}
	break;
</pre>
<p>And then slightly modify the _isDuplicate method:</p>
<pre lang="php">
foreach ($this->getContainer() as $item) {
    if (($item->source === null)
        && array_key_exists('src', $item->attributes)
        && ($file == $item->attributes['src']))
    {
        return true;
    }
    if ($item->source == $file) {
        return true;
    }
}
return false;
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/04/27/inline-scripts-with-zend_view_helper_inlinescript/" rel="bookmark" title="Inline Scripts with Zend_View_Helper_InlineScript">Inline Scripts with Zend_View_Helper_InlineScript </a></li>
<li><a href="/2010/06/24/zend-framework-inject-javascript-code-in-a-actionview/" rel="bookmark" title="Zend Framework: Inject JavaScript Code in a Action/View">Zend Framework: Inject JavaScript Code in a Action/View </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
<li><a href="/2010/05/25/download-files-with-zend-framework/" rel="bookmark" title="Download Files with Zend Framework">Download Files with Zend Framework </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/06/30/zend_view_helper_inlinescript-appends-scripts-twice/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
