Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting property of zend view using a helper called in the layout script
    primarykey
    data
    text
    <p>I've written a helper in order to make my main nav. Part of this helper checks if there is a user logged in by checking if a user has been assigned to the view</p> <pre><code>if($this-&gt;view-&gt;user) { //do stuff } </code></pre> <p>However when I call this helper in my layout script it is unable to access $this->view->user; it has NULL value (although var_dump($this->view returns the view object, jsut not with all the properties I assigned to it). It can only access the user data if, in the view script, I assign it to the layout, i.e.</p> <pre><code>$this-&gt;layout()-&gt;user = $this-&gt;user; </code></pre> <p>And then in the helper do</p> <pre><code>if($this-&gt;view-&gt;layout()-&gt;user) { //do stuff } </code></pre> <p>Which is a pain as it means in each view script I have to add a line to pass the data to the layout.</p> <p>Is there a way to enable helpers called in the layout script to access properties of the view?</p> <p>edit*</p> <p>Here's the source:</p> <pre><code> &lt;?php class Zend_View_Helper_MainNav extends Zend_View_Helper_Abstract { public function mainNav() { if ($this-&gt;view-&gt;layout()-&gt;navType == 'none'){ $html = '&lt;div id="nav"&gt;&lt;a href="/" id="logo"&gt;Sum puzzles&lt;/a&gt;&lt;/div&gt;'; } else if($this-&gt;view-&gt;layout()-&gt;navType == 'small'){ $html = '&lt;div id="nav" class="small"&gt;&lt;a href="/" id="logo"&gt;Sum puzzles&lt;/a&gt;&lt;ul&gt;' . '&lt;li&gt;&lt;a href="/puzzle/view" class="button"&gt;play a puzzle&lt;/a&gt;&lt;/li&gt;' . '&lt;li&gt;&lt;a href="/sequence/play" class="button"&gt;puzzle sequences&lt;/a&gt;&lt;/li&gt;' . '&lt;li&gt;&lt;a href="/puzzle/create" class="button"&gt;create a puzzle&lt;/a&gt;&lt;/li&gt;'; if(!$this-&gt;view-&gt;user) { $html .= '&lt;li&gt;&lt;a href="/teachers" class="button"&gt;teachers area&lt;/a&gt;&lt;/li&gt;'; } $html .='&lt;/ul&gt;&lt;/div&gt;'; } else if($this-&gt;view-&gt;layout()-&gt;navType == 'large'){ $html = '&lt;div id="nav" class="large"&gt;&lt;a href="/" id="logo"&gt;Sum puzzles&lt;/a&gt;&lt;ul&gt;' . '&lt;li&gt;&lt;a href="/sequence/play" class="playSequence"&gt;Play a sequence of puzzles&lt;/a&gt;&lt;/li&gt;' . '&lt;li&gt;&lt;a href="/puzzle/create" class="createPuzzle"&gt;Create your own puzzles&lt;/a&gt;&lt;/li&gt;'; if(!$this-&gt;view-&gt;user) { $html .= '&lt;li&gt;&lt;a href="/teachers" class="teachersArea"&gt;teachers area&lt;/a&gt;&lt;/li&gt;'; } $html .= '&lt;/ul&gt;&lt;/div&gt;'; } return $html; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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