gpt4 book ai didi

zend-framework - Zend 框架 : what is the difference between partials and placeholders

转载 作者:行者123 更新时间:2023-12-04 06:50:23 26 4
gpt4 key购买 nike

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

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

在什么情况下应该使用部分,什么场景是占位符的最佳使用?

最佳答案

这很简单,占位符用于在 View 和布局之间持久化数据,而部分用于在特定 View 中执行特定任务。
请参阅引用资料中的这些摘录。

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

在这种情况下,菜单 ID 是在 Bootstrap 中设置的,但这并不需要它只是简单的。
protected function _initMenus() {

$view = $this->getResource('view');
$view->mainMenuId = 4;
$view->adminMenuId = 5;
}

[编辑]

我认为可能需要一个更好的占位符示例。这个占位符是一个小型搜索表单,我在不同配置的多个 Controller 中的多个操作中使用。

在此配置中,此表单设置为仅搜索音乐艺术家,使用此占位符的 Controller 将具有不同的 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;
}

我在布局中使用占位符(也可以在任何 View 脚本中使用)。搜索表单在占位符有值时呈现,而在占位符没有值时不呈现。
//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>

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

关于zend-framework - Zend 框架 : what is the difference between partials and placeholders,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9206752/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com