根据我的理解,可以使用占位符和部分来呈现特定的模板/容器.
在哪种情况下应该使用部分情况,哪种情况最适合占位符?
请参阅参考文献中的这些摘录.
77.4.1.6. Placeholder Helper: The Placeholder view helper is used to persist content between view scripts and view instances. It also
offers some useful features such as aggregating content,capturing
view script content for later use,and adding pre- and post-text to
content (and custom separators for aggregated content).77.4.1.5. Partial Helper: The Partial view helper is used to render a specified template within its own variable scope. The primary use is
for reusable template fragments with which you do not need to worry
about variable name clashes. Additionally,they allow you to specify
partial view scripts from specific modules.
这是一个占位符的简单示例,听起来像你想要的. (保持数据)
<?PHP //this is placed in my layout above the html and uses an action helper //so that a specific action is called,you could also use view helpers or partials $this->layout()->nav = $this->action('render','menu',null,array('menu' => $this->mainMenuId)) ?> <div id="nav"> //Here the placeholder is called in the layout <?PHP echo $this->layout()->nav ?> </div>
在这种情况下,菜单ID是在bootstrap中设置的,但是这并不简单.
protected function _initMenus() { $view = $this->getResource('view'); $view->mainMenuId = 4; $view->adminMenuId = 5; }
[编辑]
我认为一个更好的占位符示例可能是有序的.这个占位符是一个小的搜索表单,我在不同配置的几个控制器中的几个动作中使用.
在此配置中,此表单设置为仅搜索音乐艺术家,使用此占位符的控制器将具有setAction()的不同路径,不同的标签以及有时不同的占位符文本.我使用相同的表单来搜索音乐和视频数据库.
我总是使用相同的设置或者更喜欢这样做,然后我可以将其设置为插件.
//in the controller public function preDispatch() { //add form $searchForm = new Application_Form_Search(); $searchForm->setAction('/admin/music/update'); $searchForm->query->setAttribs(array('placeholder' => 'Search for Artist','size' => 27,)); $searchForm->search->setLabel('Find an Artist\'s work.'); $searchForm->setDecorators(array( array('ViewScript',array( 'viewScript' => '_searchForm.phtml' )) )); //assign form to placeholder $this->_helper->layout()->search = $searchForm; }
我在布局中使用占位符(也可以在任何视图脚本中使用).当占位符具有值时呈现搜索表单,而当占位符没有值时不呈现搜索表单.
//in the layout.phtml <?PHP echo $this->layout()->search ?>
并且只是为了完成,这里是表单用作viewscript装饰器的部分.
<article class="search"> <form action="<?PHP echo $this->element->getAction() ?>" method="<?PHP echo $this->element->getMethod() ?>"> <table> <tr> <th><?PHP echo $this->element->query->renderLabel() ?></th> </tr> <tr> <td><?PHP echo $this->element->query->renderViewHelper() ?></td> </tr> <tr> <td><?PHP echo $this->element->search ?></td> </tr> </table> </form> </article>
这个例子应该真正说明部分和占位符之间的区别.