开发者

Is there a way to have a navigation entry with no link in zend navigation?

开发者 https://www.devze.com 2023-03-26 20:58 出处:网络
I\'m sure my question is pretty straight forward, and I\'ve been looking for an answer to this, but I can\'t seem to开发者_如何学JAVA make it work. I want to do something like this:

I'm sure my question is pretty straight forward, and I've been looking for an answer to this, but I can't seem to开发者_如何学JAVA make it work. I want to do something like this:

<?xml version="1.0" encoding="UTF-8"?>
<configdata>
<dashboard>
    <label>Dashboard</label>
    <controller>dashboard</controller>
    <action>index</action>
    <module>global</module>
</dashboard>
<bills>
    <label>Bills</label>
    <pages>
        <create-bill>
            <label>Create New Bill</label>
            <controller>bill</controller>
            <action>create</action>
            <module>global</module>
        </create-bill>
    </pages>
</bills>
</configdata>

Please note that in the <bills> section, I want to have a category with just a label, that way when I add styling later, I can hover over "Bills" and "Create New Bill" and other links will be shown, but clicking on "Bills" shouldn't do anything because it's just a category header.

I hope that makes sense.

Is there a way to have a navigation entry with no link in zend navigation?


You must specify a type for your page otherwise Zend_Navigation will throw an exception. In cases like yours I always use a Zend_Navigation_Page_Uri as page type and specify its uri to #. To apply this to your config file you could do this

<bills>
    <label>Bills</label>
    <uri>#</uri>
    <pages>
        <create-bill>
            <label>Create New Bill</label>
            <controller>bill</controller>
            <action>create</action>
            <module>global</module>
        </create-bill>
    </pages>
</bills>

The generated markup still contains a link but it will not point anywhere.

Moreover, since you need to bind some javascript to it in order to show the menu, you could even disable it by returning false in the click handler for that links.

In order to attach javascript callbacks (or some css) to that kind of link you may find useful to attach a class to those links. Within the same configuration file, you could with this code

<bills>
    <label>Bills</label>
    <uri>#</uri>
    <class>fakelink</class>
    <pages>
        <create-bill>
            <label>Create New Bill</label>
            <controller>bill</controller>
            <action>create</action>
            <module>global</module>
        </create-bill>
    </pages>
</bills>

In this case the generated markup would be

<li class="fakelink>
  <a href="#">Bills</a>
  <ul>submenu here</ul>
</li>

and you could easily select that kind of links with a javascript library. For example with jQuery you could do this:

$(function() { $('.fakelinks > a').click(function () { return false; }); });


There is actually another way to solve this.

You can set custom properties on all Zend_Navigation_Page just by defining extra configuration options in your xml/array/...

Then by using a dedicated partial to render your menu/breadcrumbs you can perfectly skip rendering the <a> tag or render a completely different markup based on these properties.

<!-- ... -->
    <page1>
        <label>Page 1</label>
        <uri>page1</uri>
        <link>false</link> <!-- Custom property -->
        <pages>
            <page1_1>
                <label>Page 1.1</label>
                <uri>page1/page1_1</uri>
            </page1_1>
            <page1_2>
                <label>Page 1.2</label>
                <uri>page1/page1_2</uri>
            </page1_2>
            <page1_3>
                <label>Page 1.3</label>
                <uri>page1/page1_3</uri>
            </page1_3>
        </pages>
    </page1>
<!-- ... -->

Note: I'm using the breadcrumbs example here but most of the Navigation View_Helpers have a setPartial() method, including the menu helper.

Then in your view script or layout you just specify that your breadcrumbs helper needs to use a partial.

<?php
echo $this->navigation()->breadcrumbs()->setPartial('my_breadcrumbs.phtml');

And in your partial you loop over the pages in year breadcrumb trail and check the custom properties for each page.

<?php
foreach($this->pages as $page)
{
    $properties = $page->getCustomProperties();

    // Check if we need to render the link tag
    if($properties['link'] !== false){
        echo '<a href="' . $page->getHref() . '">';
    }

    // Render the label
    echo $page->getLabel();

    // And check if we need to render the closing tag
    if($properties['link'] !== false){
        echo '</a>';
    }

}

Note: By using a partial you'll lose some default functionality of the Breadcrumb View_Helper like setLinkLast, setSeparator, ... but these shouldn't pose too much of a problem.


If you're happy for your category labels to be spans then just specifying an empty URI will do the job. Zend_View_Helper_Navigation_Menu::htmlify() is what renders a Zend_Navigation_Page:

/**
 * Returns an HTML string containing an 'a' element for the given page if
 * the page's href is not empty, and a 'span' element if it is empty
 *
 * Overrides {@link Zend_View_Helper_Navigation_Abstract::htmlify()}.
 *
 * @param  Zend_Navigation_Page $page  page to generate HTML for
 * @return string                      HTML string for the given page
 */
public function htmlify(Zend_Navigation_Page $page)

Example output:

<ul class="navigation">
    <li>
        <span id="menu-staff">Staff</span>
        <ul>
            <li>
                <a href="/staff/holiday/book-holiday" id="menu-staff-holiday-book">Book Holiday</a>
            </li>
            <li>
                <a href="/staff/holiday/view-holidays" id="menu-staff-holiday-view-booked">View Booked and Remaining Holiday</a>
            </li>
        </ul>
    </li>
</ul>


I went about it in a different way:

nav:

  User:
    label: Account Services
    uri: @fakeUri

Then in my template code:

<?php foreach ($navSettings as $navSetting): ?>
    <?php if ('@fakeUri' === $navSetting->getUri()): ?>
        <?php echo $navSetting->getLabel() ?>
    <?php else: ?>
        <a href="<?php echo $navSetting->getUri() ?>"><?php echo $navSetting->getLabel()) ?>
    <?php endif ?>
<?php endforeach ?>


<page_bills>
   <label>Bills</label>
   <type>uri</type>
   <pages>

   </pages>
</page_bills>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号