在 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 匹配,因此我需要为每个章节和每个部分复制这些模板.
在不重复模板的情况下执行此操作的正确方法是什么?
最佳答案
我认为您不需要为每个菜单创建一个模板。我通过使用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 %}
并添加了menu/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 %}
关于python - 夹层的菜单页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29823990/