Where to put code required by the layout in Zend Framework?
I 开发者_C百科am using Zend_Layout for the layout of my Zend Framework application. It is very simple, but I still have a few operations that I need to do in the layout. For now, the code is included between PHP brackets, but I feel like this is not really the cleanest way to go.
I saw people using plugins, but I don't think this is the way I want to go. Oviously, I could extract the "complicated" part, do it in a nice action/controller manner, and use a placeholder in the layout. Is this the way to go ? Do you have examples of such things (for instance the navigation menu delegated in its own action) ?
navigation()
is a concrete implementation of placeholder view helper.
You may create the same placeholder implementation, or use
- include('myhtml.phtml');
$this->render('script.phtml');
- partials
- partialLoops
- placeholders
- view helpers
It depends what do you need.
Action stack is evil, as you probably already heard.
As takeshin said, the navigation() placeholder is probably the way to go. However, I did not want to go through the setup of Zend_Navigation for this particular case, so I used a custom view helper.
application/views/helpers/Menu.php
:
<?php
class Zend_View_Helper_Menu extends Zend_View_Helper_Abstract {
public function menu() {
// my code ...
$this->view->menu = $menu;
return $this->view->render('helpers/menu.phtml');
}
}
application/views/scripts/helpers/menu.phtml
:
<ul>
<?php foreach ($this->menu as $item) { // print menu } ?>
</ul>
This way, I can simply call this helper from my layout :
...
<div id="menu">
<?php echo $this->menu(); ?>
</div>
...
精彩评论