ZF2 custom attributes in navigation

后端 未结 3 1868
渐次进展
渐次进展 2021-02-04 14:09

How would I add custom attributes into Zend Framework 2 navigation?
I know I can add id or class -> but that\'s about it....

1) How would I add data-test=\'bla

3条回答
  •  野性不改
    2021-02-04 14:43

    The Page classes have some dedicated setters for common attributes (setLabel, setId, setUri etc), If a setter not exists __set will be called. See the manual for more information about this and also about extending the AbstractPage class.

    array(
        'label' => 'Page 1',
        'id' => 'home-link',
        'uri' => '/',
        'data-test' => 'blahblah'
    ),
    

    Now you can do $page->get('data_test') and it will return blahblah.

    Your second question is about altering the rendering of the menu (adding a attribute to the li. ZF2 is using the menu view helper to render a navigation menu. All the navigation view helpers have an option to use your own partial view for rendering using setPartial().

    In your viewscript:

    $partial = array('menu.phtml', 'default');
    $this->navigation()->menu()->setPartial($partial);
    echo $this->navigation()->menu()->render();
    

    In your partial view menu.phtml do something like this:

      container as $page): ?>
    • navigation()->menu()->htmlify($page)?>

    This will only render the highest level of the menu. If you have deeper/nested structure your custom view script will end up far more complex.

    Hope this helps.

提交回复
热议问题