如何设置 Zend_View_Helper_Navigation_XXX 使用的 id 前缀
当我在视图中渲染 Zend_Nagivation
实例时,默认情况下,锚标记会使用视图助手的前缀分配 id,后跟破折号,然后是页面的 id。
第 1 页的锚点 id 示例(全部使用相同的 Zend_Nagivation
实例):
Zend_View_Helper_Navigation_Menu
= "menu-1- "
Zend_View_Helper_Navigation_Breadcrumbs
= "breadcrumbs-1" My_View_Helper_Navigation_MyMenu
= "mymenu-1"
这对于大多数情况来说都是完美的,但我想手动设置该前缀,但找不到方法。
解决方案
指定前缀可以通过添加以下代码,然后在渲染时调用setIdPrefix()
来完成:
class My_View_Helper_Navigation_MyMenu extends Zend_View_Helper_Navigation_Menu
{
protected $_idPrefix = null;
/**
* Set the id prefix to use for _normalizeId()
*
* @param string $prefix
* @return My_View_Helper_Navigation_MyMenu
*/
public function setIdPrefix($prefix)
{
if (is_string($prefix)) {
$this->_idPrefix = $prefix;
}
return $this;
}
/**
* Use the id prefix specified or proxy to the parent
*
* @param string $value
* @return string
*/
protected function _normalizeId($value)
{
if (is_null($this->_idPrefix)) {
return parent::_normalizeId($value);
} else {
return $this->_idPrefix . '-' . $value;
}
}
}
When I render a Zend_Nagivation
instance in my view, by default the anchor tags have id's assigned with the prefix of the view helper, followed by a dash, and then the page's id.
Examples of Page 1's anchor id (all using the same Zend_Nagivation
instance):
Zend_View_Helper_Navigation_Menu
= "menu-1"Zend_View_Helper_Navigation_Breadcrumbs
= "breadcrumbs-1"My_View_Helper_Navigation_MyMenu
= "mymenu-1"
This is perfect for most cases, but I'd like to set that prefix manually, and I can't find a way to do it.
Solution
Specifying the prefix can be accomplished by adding the following code, and then calling setIdPrefix()
when rendering:
class My_View_Helper_Navigation_MyMenu extends Zend_View_Helper_Navigation_Menu
{
protected $_idPrefix = null;
/**
* Set the id prefix to use for _normalizeId()
*
* @param string $prefix
* @return My_View_Helper_Navigation_MyMenu
*/
public function setIdPrefix($prefix)
{
if (is_string($prefix)) {
$this->_idPrefix = $prefix;
}
return $this;
}
/**
* Use the id prefix specified or proxy to the parent
*
* @param string $value
* @return string
*/
protected function _normalizeId($value)
{
if (is_null($this->_idPrefix)) {
return parent::_normalizeId($value);
} else {
return $this->_idPrefix . '-' . $value;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
罪魁祸首是
Zend_View_Helper_Navigation_HelperAbstract::_normalizeId()
,除了为您需要的每个导航视图助手创建您自己的自定义版本之外,我没有其他解决方案。The culprit is
Zend_View_Helper_Navigation_HelperAbstract::_normalizeId()
for which I see no other solution than to create your own custom version of each navigation view helper you require.