1

我尝试用 Velocity 制作新的 Liferay 主题。我想制作多级菜单。我可以像这样用手制作关卡:

    <ul class="obsm">
    #foreach ($nav_item in $nav_items)
        #if ($nav_item.isSelected())
            <li class="active">
        #else
            <li>
        #end
            <a href="$nav_item.getURL()" $nav_item.getTarget()><span>$nav_item.icon() $nav_item.getName()</span></a>
            #if ($nav_item.hasChildren())
                <ul>
                    #foreach ($nav_child in $nav_item.getChildren())
                        #if ($nav_child.isSelected())
                            <li class="active">
                        #else
                            <li>
                        #end
                            <a href="$nav_child.getURL()" $nav_child.getTarget()>$nav_child.getName()</a>
                            #if ($nav_child.hasChildren())
                                <ul>
                                    #foreach ($nav_child in $nav_child.getChildren())
                                        #if ($nav_child.isSelected())
                                            <li class="active">
                                        #else
                                            <li>
                                        #end
                                            <a href="$nav_child.getURL()" $nav_child.getTarget()>$nav_child.getName()</a>
                                        </li>
                                    #end
                                </ul>
                            #end
                        </li>
                    #end
                </ul>
            #end
        </li><br>
    #end
</ul>

有 3 级菜单。但它并不好。我想做无限关卡菜单。这是可能的?

4

2 回答 2

2

我建议创建一个速度宏并递归调用它。像这样:

#macro ( navigation $list )
    #foreach ( $item in $list )
        //code

        #if ( $item.hasChilden() )
            #navigation ( $item.getChildren() )
        #end
    #end    
#end

请参阅:http: //velocity.apache.org/engine/releases/velocity-1.5/user-guide.html#velocimacros

于 2013-01-24T01:02:17.930 回答
0

我有另一个解决方案

<nav class="$nav_css_class header-menu" id="navigation" role="navigation">
#set ($nav_item_selected="")
#set ($nav_item_2level_selected="")
#set ($nav_item_3level_selected="")
<ul class="menu-level-01" aria-label="#language ("site-pages")" role="menubar">
    #foreach ($nav_item in $nav_items)
        #set ($nav_item_attr_has_popup="")
        #set ($nav_item_attr_selected="")
        #set ($nav_item_css_class = "")
        #set ($nav_item_css_class_active = "")

        #if ($nav_item.isSelected())
            #set ($nav_item_attr_selected="aria-selected='true'")
            #set ($nav_item_css_class = "selected")
            #set ($nav_item_css_class_active = "active")
            #set ($nav_item_selected = $nav_item)
        #end

        #if ($nav_item.hasChildren())
            #set ($nav_item_attr_has_popup="aria-haspopup='true'")
        #end

        <li class="$nav_item_css_class $nav_item_css_class_active" id="layout_$nav_item.getLayoutId()" $nav_item_attr_selected role="presentation">
            <a aria-labelledby="layout_$nav_item.getLayoutId()" href="$nav_item.getURL()" $nav_item_attr_has_popup $nav_item.getTarget() role="menuitem"><span>$nav_item.icon() $nav_item.getName()</span></a>

        </li>
    #end
</ul>
#if ($nav_item_selected)
    #if ($nav_item_selected.hasChildren())
        <ul class="menu-level-02" aria-label="#language ("site-pages")" role="menubar">
            #foreach ($nav_item_2level in $nav_item_selected.getChildren())
                #set ($nav_item_2level_attr_selected="")
                #set ($nav_item_2level_css_class = "false")
                #set ($nav_item_2level_css_class_active = "")

                #if ($nav_item_2level.isSelected())
                    #set ($nav_item_2level_attr_selected="aria-selected='true'")
                    #set ($nav_item_2level_css_class = "selected")
                    #set ($nav_item_2level_css_class_active = "active")
                    #set ($nav_item_2level_selected = $nav_item_2level)
                #end

                #if ($nav_item_2level.hasChildren())
                    #set ($nav_item_2level_attr_has_popup="aria-haspopup='true'")
                #end

                <li class="$nav_item_2level_css_class $nav_item_2level_css_class_active" id="layout_$nav_item_2level.getLayoutId()" $nav_item_2level_attr_selected role="presentation">
                    <a aria-labelledby="layout_$nav_item_2level.getLayoutId()" href="$nav_item_2level.getURL()" $nav_item_2level_attr_has_popup $nav_item_2level.getTarget() role="menuitem"><span>$nav_item_2level.icon() $nav_item_2level.getName()</span></a>

                </li>
            #end
        </ul>

        #if ($nav_item_2level_selected.hasChildren())
            <ul class="menu-level-03" aria-label="#language ("site-pages")" role="menubar">
                #foreach ($nav_item_3level in $nav_item_2level_selected.getChildren())
                    #set ($nav_item_3level_attr_selected="")
                    #set ($nav_item_3level_css_class = "false")
                    #set ($nav_item_3level_css_class_active = "")

                    #if ($nav_item_3level.isSelected())
                        #set ($nav_item_3level_attr_selected="aria-selected='true'")
                        #set ($nav_item_3level_css_class = "selected")
                        #set ($nav_item_3level_css_class_active = "active")
                        #set ($nav_item_3level_selected = $nav_item_3level)
                    #end

                    #if ($nav_item_3level.hasChildren())
                        #set ($nav_item_3level_attr_has_popup="aria-haspopup='true'")
                    #end

                    <li class="$nav_item_3level_css_class $nav_item_3level_css_class_active" id="layout_$nav_item_3level.getLayoutId()" $nav_item_3level_attr_selected role="presentation">
                        <a aria-labelledby="layout_$nav_item_3level.getLayoutId()" href="$nav_item_3level.getURL()" $nav_item_3level_attr_has_popup $nav_item_3level.getTarget() role="menuitem"><span>$nav_item_3level.icon() $nav_item_3level.getName()</span></a>

                    </li>
                #end
            </ul>   
        #end    
    #end    
#end    

于 2015-04-28T08:02:13.707 回答