Zend Framework 2导航。递归地将ul类添加到子菜单

时间:2014-08-28 21:59:50

标签: php html navigation zend-framework2 view-helpers

我正在使用视图助手导航并尝试将类添加到子菜单。 方法setUlClass()仅为菜单的第一级添加类。

<div id="sidebar-left" class="col-xs-2 col-sm-2">
    <ul class="nav main-menu">
        <li class="active">
            <a href="/administrator">
                <i class="fa fa-dashboard"></i>
                <span class="hidden-xs"> Home</span>
            </a>
        </li>
        <li>
            <a href="/administrator/content/news">
                <i class="fa fa-bullhorn"></i>
                <span class="hidden-xs"> News</span>
            </a>
            <ul>   <!-- <-- here want to add class -->
                <li>
                    <a href="/administrator/content/add-news">
                        <i class="fa fa-plus"></i>
                        <span class="hidden-xs"> Add News</span>
                    </a>
                </li>
            </ul>
        </li>
    </ul> 
</div>

module.config.php文件如下:

'navigation' => array(
    'default' => array(
        array(
            'label' => '<i class="fa fa-bullhorn"></i><span class="hidden-xs"> News</span>',
                'route' => 'admin/default',
                'controller' => 'content',
                'action' => 'news',
                'resource'  => 'Admin\Controller\Admin',
                'pages' => array(
                    array(
                        'label' => '<i class="fa fa-plus"></i><span class="hidden-xs"> Add News</span>',
                        'route' => 'admin/default',
                        'controller' => 'content',
                        'action' => 'add-news',
                        'resource'  => 'Admin\Controller\Admin',
                    )
                )
            ),
        ),
    ),

在视图中调用渲染方法:

<?php echo $this->navigation('navigation')->menu()->setUlClass('nav main-menu')->escapeLabels(false); ?>

怎么做?

1 个答案:

答案 0 :(得分:0)

我认为ZF2中的类代码与ZF1相同。 并且通过设置配置选项没有解决方案,你在\ Zend \ View \ Helper \ Navigation \ Menu :: renderNormalMenu()中看到这个代码块:

if ($depth > $prevDepth) {
    // start new ul tag
    if ($ulClass && $depth ==  0) {
        $ulClass = ' class="' . $escaper($ulClass) . '"';
    } else {
        $ulClass = '';
    }
        $html .= $myIndent . '<ul' . $ulClass . '>' . PHP_EOL;  
 }

btw css可以应用于子ul标签:

ul.nav.main-menu ul {
  ...
}