Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Rather than use the action stack and the <code>action()</code> view helper, you could render a "partial view script" that includes your sidebar elements.</p> <pre><code># in your layout.phtml &lt;div id="sidebar"&gt; &lt;?php echo $this-&gt;render('blog/_sidebar.phtml'); /*relative to your view scripts directory*/ ?&gt; &lt;/div&gt; # in blog/_sidebar.phtml &lt;div id="blog_categories"&gt; &lt;?php foreach ($this-&gt;categories as $category): ?&gt; &lt;?php echo $category-&gt;name; ?&gt; &lt;?php endforeach; ?&gt; &lt;/div&gt; </code></pre> <p>The <code>render()</code> view helper is used to render the content of another view script. It has the same scope as all your other view scripts, so if there are any variable assigned to the view, they will be available to your partial. So in the example above, the categories variable was set in the controller.</p> <p>There is another view helper called the <code>partial()</code> view helper. This function is a little more expensive since it creates its own variable scope. In other words, none of your current view variables will be available. You will have a clean slate to work with, which means you must pass in any variables you need:</p> <pre><code># in your layout.phtml &lt;div id="sidebar"&gt; &lt;?php echo $this-&gt;partial('blog/_sidebar.phtml', array('categories2'=&gt;$this-&gt;categories)); ?&gt; &lt;/div&gt; # in blog/_sidebar.phtml &lt;div id="blog_categories"&gt; &lt;?php foreach ($this-&gt;categories2 as $category): ?&gt; &lt;?php echo $category-&gt;name; ?&gt; &lt;?php endforeach; ?&gt; &lt;/div&gt; </code></pre> <p>I don't find myself using <code>partial()</code> very often since it is more expensive, and I rarely need to create a separate context.</p> <p>As far as setting up the variables for use in the sidebar partial (<code>$this-&gt;categories</code> in this example), I have used a number of different methods depending on the particular problem. If it's specific to a controller action, I will write the code and assign it in the view script:</p> <pre><code># controller public function somethingAction() { $this-&gt;view-&gt;categories = $this-&gt;_getCategoriesForThisParticularAction(); // other controller code } </code></pre> <p>If my code is more generic to all the actions of the controller, I will utilize the controller's <code>preDispatch()</code> function. If it's more generic to multiple controllers, I will put the code in the <code>init()</code> of my base controller (a controller the most of my controllers extend).</p> <p>Sometimes I do not even put the code in my controller. If it's simple enough, I just stick the code in the partial. If it's a little more complex, I will move it to a view helper. This may break the MVC pattern, but I think it really depends on the particular case in order to determine the best placement.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload