获取Zend的导航菜单使用jQuery鱼眼镜头到工作
-
12-09-2019 - |
题
我使用Zend_Navigation(甜除了框架,顺便说一句)建立我的菜单后,它应该在的页面(不言自明)呈现。我首先在控制器某处设置容器:
// $pages is the array containing all page information
$nav = new Zend_Navigation($pages);
$this->view->navigation($nav);
然后,在布局,它呈现像这样:
echo $this->navigation()->menu();
其中完美地工作。现在:我希望菜单进行不同的呈现了一点。我正在创建的页面使用了jQuery 鱼眼插件打造一台Mac状底座菜单。但是,该插件需要一个特定标记...
实际上,它需要同时含有<a>
(用于图标)和一个<img>
(对于工具提示)<span>
元素的列表。标准菜单视图助手呈现一个无序列表(逻辑上)内的一切,与'label'
参数作为链接文本。
这似乎传递给'label'
参数内容呈现之前逃脱,所以插入HTML有不会做我任何好处。此外,鱼眼一般似乎并没有采取包含在<li>
标签的物品,用包裹在<ul></ul>
整个事情,而只是一个<a>
要素之一级列表。
我在想编写自定义视图助手的被告席上,这将能够采取插入<img>
和<span>
的照顾,但我有一个非常困难的时间去连接到导航类的自定义视图助手。我只是想不通哪里放置它,并以何种方式,即使我所有自定义类(模型和等)由自动被甜蜜的照顾。在此任何想法?
话又说回来,就算我能得到这个视图助手的工作,我还是留下与HTML无序列表 - 我知道我可以失去一个也使用自定义视图助手,但我一直是个迷含主导航菜单列表内,为语义的缘故。
如果任何人都可以帮助我的话,我会非常感激。如果鱼眼只是并不意味着与<ul>
的工作,那简直是太糟糕了......会有在这种情况下完全失去Zend_Navigation一个很好的理由?
解决方案
我还没有看到的方式但无论是在制造定制渲染器的条款。下面是我落得这样做虽然:
首先,代替再现默认菜单()调用,创建部分渲染成:
// menu.phtml is partial, cms is module
$partial = array('menu.phtml', 'cms');
$this->navigation()->menu()->setPartial($partial);
echo $this->navigation()->menu()->render();
然后(来自文档),则可以创建一个“定制”渲染像这样:
// -- inside menu.phtml
foreach ($this->container as $page)
{
// this just prints a "<a>" or "<span>" tag
// depending on whether the page has a uri
echo $this->menu()->htmlify($page), PHP_EOL;
}
我最终使我本来什么(一个菜单,子菜单的一个级别),这个脚本:
// -- another menu.phtml
<ul class="navigation">
<?php
$html = array();
foreach ($this->container as $page)
{
$html[] = "<li>";
$html[] = $this->menu()->htmlify($page) . PHP_EOL;
if (!empty($page->pages))
{
$html[] = "<ul>";
foreach ($page->pages as $subpage)
{
$html[] = "<li>";
if ($href = $subpage->getHref()) $html[] = "<a href=\"{$href}\">";
else $html[] = "<span>";
$html[] = "<img src=\"/ui/cms/img/icons/edit.png\" alt=\"\"/>";
$html[] = $subpage->getLabel();
if ($href) $html[] = "</a>";
else $html[] = "</span>";
$html[] = "</li>";
}
$html[] = "</ul>";
}
$html[] = "</li>";
}
echo join(PHP_EOL, $html);
?>
</ul>
您可以只改变标记的部分,并添加您的图像/图标那里。
其他提示
对不起,用于在以前的帖子的格式问题
要添加自定义的助手做到以下几点,你的引导:
protected function _initNavigation()
{
$this->bootstrap('view'); // make sure we have a view
$view = $this->getResource('view'); // get the view resource
$config = new Zend_Config_Xml(APPLICATION_ROOT . '/config/navigation.xml','nav');
$container = new Zend_Navigation($config);
$view->navigation($container);
// Tell Zend were it can find your custom helpers and what
// the prefix is going to be.
$view->addHelperPath(
APPLICATION_ROOT . '/library/MyApp/View/Helper/Navigation',
'MyApp_View_Helper_'
);
}
接下来创建自定义视图助手,并把它放在“addHelperPath”路径。根据你想做什么,你需要至少实现自己htmlify功能。看一看的Zend /视图/帮助/导航/ Menu.php,例如,
class MyApp_View_Helper_MyMenu extends Zend_View_Helper_Navigation_Menu
{
public function myMenu(Zend_Navigation_Container $container = null)
{
if (null !== $container) {
$this->setContainer($container);
}
return $this;
}
protected function htmlify(Zend_Navigation_Page $page )
{
...
}
}
接下来,在您的PHTML脚本:
<?php echo $this->navigation()->myMenu()->setMaxDepth(0); ?>
或只是
<?php echo $this->navigation()->myMenu(); ?>
现在,如果你想自己的自定义属性添加到生成的HTML你可以将它们添加到您的导航INI或XML文件。例如:
<home>
<label>Home</label>
<uri>/</uri>
<img>
<src>/images/MyIcon.gif</src>
<img>
</home>
自定义XML标签<img>
将被存储为所述导航页面的属性,并且可以取这样的:
foreach ($iterator as $page) {
...
$properties = new Zend_Config($page->GetCustomProperties());
$myPicture = $properties->img->src;
...
}
希望这有助于。 彼得