Zend Framework:部分和占位符之间有什么区别

问题描述:

在Zend Framework中,任何人都可以解释partial和placeholder之间的区别吗?Zend Framework:部分和占位符之间有什么区别

从我的理解,可以通过使用占位符和部分来呈现特定的模板/容器。

在哪种情况下应该使用部分和什么情况是占位符的最佳用法?

这非常简单,placholder用于在视图和布局之间保存数据,部分在特定视图中用于执行特定任务。
请参阅参考文献的摘录。

77.4.1.6。占位符助手:占位符视图助手用于在视图脚本和视图实例之间保存内容。它也 提供了一些有用的功能,如聚合内容,捕获 查看脚本内容供以后使用,以及向 内容(以及聚合内容的自定义分隔符)添加前后文本。

77.4.1.5。部分助手:部分视图助手用于在其自己的变量范围内呈现指定的模板。主要用途是 可重复使用的模板片段,您不必担心 有关变量名称冲突。此外,它们允许您指定 特定模块的部分视图脚本。

下面是占位符的一个简单示例,它听起来像你想要的。 (坚持数据)

<?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 ?>&nbsp; 
    </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> 

这个例子应该说明部分和占位符的区别。

+0

感谢您的解释。有两个用例的简单例子吗?我有一个要求,我需要在差异控制器以及模块中填充一个国家/地区。那么,如何将这个模板保存在一个地方并在整个模块和控制器中重复使用它呢? – neeraj 2012-02-09 09:01:10

我觉得你在这里可以是一个自定义的视图助手,在现有Zend_View_Helper_FormSelect类可能扩大或创建一个自定义适合自己需要的Zend表单元素什么可能更有帮助。或者,在一般位置的帮手脚本可能是最好的选择。

+0

我想要做的是从一个地方提供动态的国家数组,而不是从每个控制器的preDispatch提供。我的dropdown.phtml放在/ application/views/scripts文件夹中,我想重复使用这个phtml,使用zend部分/占位符来放置我的不同模块和控制器。 – neeraj 2012-02-09 10:51:33

+0

这是我的代码dropdown.phtml countries)&& count($ this-> countries)> 0):?> > – neeraj 2012-02-09 10:51:42