Django CMS菜单,如何将类仅设置为父类? [英] Django CMS menu, how to set a class to parents only?
问题描述
我的菜单需要一些工作.我需要在父菜单项上有一个类,但由于递归,它无法正常工作.
My menu needs some work. I need to have a class on the parent menu items, but with my recursion, it is not working.
这是我的菜单的一部分:
This is (part of) my menu:
Home
Teachers (id teachers)
Contact
Info
Projects
myproject
yourproject
我从这样的老师"开始:
I start with "teachers" like this:
{% show_menu_below_id "teqchers" 0 1 0 1 "teachers_menu.html" %}
这是我的Teachers_menu.html:
And this is my teachers_menu.html:
{% load menu_tags %}
{% for child in children %}
<li class="{% if child.selected %}selected parent_{{forloop.counter}}{% endif %} {% if child.sibling %}parent_{{forloop.counter}} {% endif %}">
<a href="{{ child.get_absolute_url }}">{{ child.get_menu_title }}</a>
{% if child.children %}
<div class="submenu">
<ul>
{% show_menu from_level to_level extra_inactive extra_active template "" "" child %}
</ul>
</div>
{% endif %}
</li>
{% endfor %}
有了这个,我的菜单就工作了一段时间.
With this in place, my menu is working for a bit.
当我单击项目"时,一切都很好,并且可以看到2个项目.但是,当我单击一个项目时,我希望页面显示出来,但是没有,它会重新构建菜单,并将所需的类添加到子元素中:
When i click Projects, all is well and the 2 projects are in view. But when i click a project, i expect the page to show, but it does not, it does rebuild my menu, and adds the needed class to the child elements:
{% if child.selected %}selected parent_{{forloop.counter}}{% endif %}
很明显,因为我想现在是个孩子,但是如何防止这种情况呢?我只需要第一个菜单项的那个类.
Obvious because it is a child now i guess, but how to prevent this? I only need that class for the first menu items.
推荐答案
而不是这样做:
{% show_menu from_level to_level extra_inactive extra_active template "" "" child %}
我现在添加了另一个这样的模板:
I now added another template like this:
{% show_menu from_level to_level extra_inactive extra_active "teachers_submenu.html" "" "" child %}
在该模板中,我现在有:
And in that template I now have:
{% load menu_tags %}
{% for child in children %}
<li class="{% if child.selected %}selected{% endif %}">
<a href="{{ child.get_absolute_url }}">{{ child.get_menu_title }}</a>
</li>
{% endfor %}
因此,额外的模板将处理子菜单.经过一些样式设置,现在可以使用.
So the extra template takes care of the submenu. With some styling it now works.
这篇关于Django CMS菜单,如何将类仅设置为父类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!