1

I'm using .ini files to render my menu.

Bootstrap

    $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/menu.ini', 'nav');
    $nav = new Zend_Navigation($config);
    $view->navigation($nav);

layout.phtml

<?php echo $this->navigation()->menu()->setMaxDepth(3) ?>

My target to reach:

<ul class="navigation" id="navigation">
    <li>

        <a id="menu-1" href="/">Home</a>
    </li>
</ul>

How to set id="navigation" for the first <ul> occurence instead of class="navigation"?

Now I've got only <ul class="navigation"> and i want to <ul id="navigation"> or <ul class="navigation" id="navigation">

G.Z
  • 121
  • 3
  • 11
  • Not sure I understand fully. What do you get now? Anyway, don't think you have anyway now to control the `id` value. Looked at `Zend_View_Helper_Navigation_Menu`? Simple to extend it and pass `$id `along with `$ulClass` that is there now. Have to ask, are you solving a really problem with this question? Maybe you just need to change your selector? – ficuscr Feb 01 '13 at 17:48
  • Now I've got only ` – G.Z Feb 01 '13 at 17:52
  • My comments stand. Look at `_renderMenu` on `Zend_View_Helper_Navigation_Menu`... `$ulClass = ' class="' . $ulClass . '"';` You will need to extend that class and write your own render code (decorator). Probably just best to extend whole class and override that method - might be an option using a partial though too. – ficuscr Feb 01 '13 at 17:57
  • Ok, thanks, but how to do this, that I've an acess to ovverided method only in for example view eg. layout.phtml? – G.Z Feb 01 '13 at 19:55
  • if this is the only change that you need you can use Javascript, is an option? – Tomas Ramirez Sarduy Feb 01 '13 at 20:25

4 Answers4

1

You would need to setup a custom view helper, in your code library, not under Zend_.

Maybe something like GZ_View_Helper_Navigation_Menu2

And then depending on how you want to configure the addition of this new view helper path, something like this in your bootstraps initView:

$view->addHelperPath('GZ/View/Helper/', 'GZ_View_Helper'); 

Or, if using Application and Resources you can setup that via the ini too with:

resources.view.helperPath.GZ_View_Helper = "GZ/View/Helper"

Your lib GZ of course needs to be on PHP's include path.

When rendering you would change the call to:

<?= $this->navigation()->menu2()->setMaxDepth(3) ?>

Although I think you can also assign a default view helper (can't find it in docs right now) to Navigation and just call

<?=  $this->navigation ?>

Related: How do I extend the Zend Navigation Menu View Helper?

Community
  • 1
  • 1
ficuscr
  • 6,975
  • 2
  • 32
  • 52
  • I've got some trouble, I can't get to my class. First. `echo APPLICATION_PATH . '/../library/App/View';` Output's `C:\Program Files\Zend\Apache2\htdocs\learning2\application/../library/App.View` How to fix thix? Eariler it worked. – G.Z Feb 01 '13 at 22:02
  • Find where you define APPLICATION_PATH and change it if need be? Use `realpath()` if you still can't tell its correct copy it into a OS X's Finder window and see if it resolves to where it should, adjust until it does. – ficuscr Feb 01 '13 at 22:06
  • Thanks a lot. `realpath()` is a solution. Thanks for links, but finally I get rid of overriding, and use JS :) Earier, I don't see your question about JS, so I'm sorry :) I think that it will be a good solution for visitors, so my apply. – G.Z Feb 02 '13 at 03:11
0

You can do this with jQuery:

$('document').load(function(){
    $("body > ul:first-child").attr('id', 'navigation');
})

EDIT:

After reading your comments, I noted that you are using Mootols library, so you can do the jQuery equivalent, something like this:

window.addEvent('domready', function() {                       
    var myMenu = new MenuMatic({ orientation:'vertical' });  
    myMenu.getElement('ul').set('id', 'navigation');              
});
Tomas Ramirez Sarduy
  • 17,294
  • 8
  • 69
  • 85
  • I wondered the same when I asked, "are you solving a really problem with this question? Maybe you just need to change your selector?". Guess OP has his reasons...? – ficuscr Feb 01 '13 at 22:04
  • @ficuscr: why should i need to change my selector? just change `div` for `nav`, `header`, or the parent id. This is a quick solution, I'm sure is not the way to go, but it works ;) – Tomas Ramirez Sarduy Feb 01 '13 at 22:22
  • http://pastebin.com/Chx57fiS I've got also this code - jquery doesn't with this. Why? I don't know. – G.Z Feb 01 '13 at 23:07
  • @G.Z: Seems like you are lost... you are using `Mootols` library, not jQuery... let's see what can I do – Tomas Ramirez Sarduy Feb 02 '13 at 02:20
  • @G.Z: I have updated the question, also I have updated your pastebin: http://pastebin.com/mPz4beyf – Tomas Ramirez Sarduy Feb 02 '13 at 02:36
  • Sorry, for me. I'm using jQuery, and plugin Mootools - for menu. Mootools was blocking jQuery functions. jQuery + Mootools = crash(Mootools only work). I resolve it by adding jQuery.noConflict()... and some other things. Also I have wrote the same function as you wrote :) Thanks guy. As you said - 2 hours ago I was lost becouse of `Mootools`. It's not... as clever as jQuery. Anyway, finally i use your solution so thanks a lot :) – G.Z Feb 02 '13 at 03:07
0

I've thought about doing this method a lot myself. I often create multiple navigation objects in my Zend Framework projects.

Instead, I would submit a different answer. In your view, enclose your call to the navigation output inside of the HTML5 nav element. This makes it both semantically clearer AND allows you to add your own ID to each section easily. So, try this:

<nav id="navigation"><?php echo $this->navigation()->menu()->setMaxDepth(3) ?></nav>

An interesting thing to note - if you only have 1 navigation element on the page, you won't even need the ID then. You can target it using the nav element in your css... for example, to make all ul's in the navigation have zero margin, you might do this in css:

nav ul { margin: 0px }
Aaron Saray
  • 1,178
  • 6
  • 19
0

In ZF 1.12 (don't know from which version exactly) there is one methode for that:

$this->navigation()->menu()->setUlId('navigation')->setMaxDepth(0);

Radke
  • 26
  • 2