让 Zend_Navigation 菜单与 jQuery 的 Fisheye 一起使用

发布于 2024-07-30 05:11:29 字数 1351 浏览 3 评论 0原文

我正在使用 Zend_Navigation (框架的甜蜜补充,顺便说一句)来构建我的菜单,之后它应该在页面中呈现(不言而喻)。 我首先将容器设置在控制器中的某个位置:

// $pages is the array containing all page information
$nav = new Zend_Navigation($pages);
$this->view->navigation($nav);

然后,在布局中,它的渲染方式如下:

echo $this->navigation()->menu();

效果非常好。 现在:我希望菜单的呈现方式略有不同。 我正在构建的页面使用 jQuery Fisheye-plugin构建一个类似 Mac 的 Dock 菜单。 然而,这个插件需要一个特定的标记...

实际上,它需要一个 元素列表,其中包含 (用于图标)和(用于工具提示)。 标准菜单视图助手(逻辑上)呈现无序列表内的所有内容,并使用 'label' 参数作为链接文本。

似乎传递给 'label' 参数的内容在渲染之前被转义,因此在那里插入 html 对我没有任何好处。 此外,鱼眼通常似乎不会将其包含在

  • 标签中的项目,而是将整个内容包裹在
      中>,但只是 元素的一级列表。
    • 我正在考虑为扩展坞编写一个自定义视图助手,它能够负责插入 ,但我我很难将自定义视图助手附加到导航类。 我只是不知道将其放置在哪里以及以什么方式放置,即使我的所有其他自定义类(模型等)都由自动加载器精心处理。 对此有什么想法吗?

      再说一次,即使我可以让这个视图助手工作,我仍然留下 HTML 无序列表 - 我知道使用自定义视图助手我也可能会丢失该列表,但我一直热衷于包含 main出于语义考虑,将导航菜单放在列表中。

      如果有人可以帮助我一点,我将不胜感激。 如果 Fisheye 不适合与

        一起使用,那就太糟糕了......在这种情况下是否有充分的理由完全失去 Zend_Navigation ?

      I'm using Zend_Navigation (sweet addition to the framework, btw) to build my menu, after which it should be rendered in the page (self-evidently). I first set the container somewhere in the controller:

      // $pages is the array containing all page information
      $nav = new Zend_Navigation($pages);
      $this->view->navigation($nav);
      

      Then, in the layout, it is rendered like this:

      echo $this->navigation()->menu();
      

      which works perfectly. Now: I want the menu to be rendered a little differently. The page I'm building uses the jQuery Fisheye-plugin to build a Mac-like Dock-menu. However, this plugin needs a specific markup...

      Actually, it takes a list of <a> elements containing both an <img> (for the icon) and a <span> (for the tooltip). The standard Menu view helper renders everything inside an unordered list (logically), with the 'label' parameter as the link text.

      It seems that content passed to the 'label' parameter is escaped before rendering, so inserting the html there won't do me any good. Additionally, the Fisheye usually doesn't seem to take its items contained in a <li> tag, with the entire thing wrapped in <ul></ul>, but just a one-level list of <a> elements.

      I was thinking about writing a custom view helper for the dock, which would be able to take care of inserting the <img> and the <span>, but I'm having a very hard time getting a custom view helper attached to the Navigation class. I just can't figure out where to place it and in what way, even though all my other custom classes (models and such) are sweetly taken care of by the autoloader. Any ideas on this?

      Then again, even if I can get this view helper to work, I'm still left with the HTML unordered list - I know I can lose that one too using the custom view helper, but I've always been a fan of containing main navigation menus inside a list, for the sake of semantics.

      If anyone can help me out a little, I'd greatly appreciate it. If the Fisheye just isn't meant to work with <ul>'s, that'd be too bad... would there be a good reason for losing Zend_Navigation altogether in this case?

      如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

      扫码二维码加入Web技术交流群

      发布评论

      需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

      评论(2

      醉南桥 2024-08-06 05:11:29

      我还没有看到制作自定义渲染器的方法。 这就是我最终所做的:

      首先,不是渲染默认的 menu() 调用,而是创建一个要渲染的部分:

      // 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>
      

      您只需更改部分中的标记并在其中添加图像/图标即可。

      I haven't seen the way yet either in terms of making a custom renderer. Here's what I ended up doing though:

      First, instead of rendering the default menu() call, create a partial to render into:

      // menu.phtml is partial, cms is module
      $partial = array('menu.phtml', 'cms');
      $this->navigation()->menu()->setPartial($partial);
      echo $this->navigation()->menu()->render();
      

      Then (from the docs), you can create a "custom" render like so:

      // -- 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;
      }
      

      I ended up making what I originally had (a menu with one level of submenus) with this script:

      // -- 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>
      

      You could just change the markup in the partial and add your images/icons there.

      蓦然回首 2024-08-06 05:11:29

      抱歉,对于上一篇文章中的格式问题

      要添加自定义帮助程序,请在引导程序中执行以下操作:

      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/View/Help/Navigation/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 标签 将存储为导航页面的属性,并且可以像这样获取:

      foreach ($iterator as $page) {
      
          ...
      
          $properties = new Zend_Config($page->GetCustomProperties());
          $myPicture = $properties->img->src;
      
          ...
      }
      

      希望这会有所帮助。
      彼得

      Sorry, for the formatting issue on the previous post

      To add your custom helper do the following, in your bootstrap:

      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_'
                );
      
      }
      

      Next create your custom view helper and put it in the ‘addHelperPath’ path. Depending on what you want to do you need to implement at least your own htmlify function. Take a look at Zend/View/Help/Navigation/Menu.php as an example.

      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 ) 
          {
              ...
          }
      }
      

      Next, in your phtml script:

         <?php echo $this->navigation()->myMenu()->setMaxDepth(0); ?>
      

      or just

         <?php echo $this->navigation()->myMenu(); ?>
      

      Now if you want to add your own custom properties to the generated HTML you can add them to your navigation ini or xml file. For example:

      <home>
        <label>Home</label>
        <uri>/</uri>
        <img>
            <src>/images/MyIcon.gif</src>
        <img>
      </home>
      

      The custom xml tag <img> will be stored as a property of the navigation page and can be fetched like:

      foreach ($iterator as $page) {
      
          ...
      
          $properties = new Zend_Config($page->GetCustomProperties());
          $myPicture = $properties->img->src;
      
          ...
      }
      

      Hope this helps.
      Peter

      ~没有更多了~
      我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
      原文