Mezzanine中的菜单页面

时间:2015-04-23 12:50:14

标签: python django content-management-system mezzanine

在Mezzanine / Django中,我有几个嵌套页面,但只有嵌入最深的页面才有内容。对于每个中间页面,我只想显示其直接子项的菜单。例如,给定布局:

Chapter 1
    Section 1.1
        Subsection 1.1.1
        Subsection 1.1.2
    Section 1.2
        Subsection 1.2.1
Chapter 2
    Section 2.1
        Subsection 2.1.1

作为chapter-1的页面应该只显示

列表
* Section 1.1
* Section 1.2

我可以通过为包含以下内容的每个章节和部分创建自定义模板来实现此目的:

{% load pages_tags %}
{% page_menu "pages/menus/chapter_menu.html" %}

其中chapter_menu.html是我的菜单模板:

{% load pages_tags %}
<ul>
{% for page in page_branch %}
{% if page.in_menu %}
{% if page.is_current_child %}
<li>
    <a href="{{ page.get_absolute_url }}">{{ page.title }}</a>
</li>
{% else %}
{% page_menu page %}
{% endif %}
{% endif %}
{% endfor %}
</ul>

但是,每个模板名称(例如pages/chapter-1.html)必须匹配它引用的页面的slug,因此我需要为每个章节和每个部分复制这些模板。

如果没有所有重复的模板,这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

我认为您不需要为每个菜单创建模板。我使用enter link description here

中的tree.html和my_menu.html解决了这个问题

所以我刚用:

替换了menus / tree.html
{% load i18n future pages_tags %}
{% spaceless %}
{% page_menu "pages/menus/my_menu.html" %}
{% endspaceless %}

并添加了菜单/ my_menu.html:

{% load pages_tags %}
{% for page in page_branch %}
    {% if page.is_current_child %}
    <a href="{{ page.get_absolute_url }}">{{ page.title }}</a>
    {% endif %}
    {% if page.is_current_or_ascendant %}
        {% if page.has_children_in_menu %}{% page_menu page %}{% endif %}
    {% endif %}
{% endfor %}