Django CMS:基于所选插件的页内导航菜单
Django CMS: In-Page Navigation Menu based on selected plugins
我在 django cms 中有一个模板,它基于我添加到内容占位符的插件创建了一个视差网站。这是我的模板:
{% extends "foundry/base.html" %}
{% load cms_tags %}
{% block title %}{% page_attribute "page_title" %}{% endblock title %}
{% block content %}
{% placeholder 'content' %}
{% endblock content %}
在base.html中,我使用{% show_menu 0 1 100 100 "foundry/menu.html" %}来生成菜单。我想根据添加到内容占位符的插件将项目添加到此菜单。因为 show_menu 在 cms 呈现占位符之前调用,所以我无法使用 NavigationNode 来注册我的菜单。如果我可以查询内容占位符中使用的插件,我就可以处理这个菜单。但是 Django CMS 数据库太复杂了,我找不到查询。
谢谢
Django CMS 提供了几个实用程序来执行此操作;你只需要挖掘源代码就可以找到它们。
from cms.templatetags.cms_tags import _get_placeholder
from cms.utils.plugins import get_plugins
if request and request.current_page:
placeholder = _get_placeholder(request.current_page, request.current_page,
template_context, placeholder_name) # placeholder_name is a string
plugins = get_plugins(request, placeholder, request.current_page.get_template())
一旦你有了占位符的插件,你就可以通过以下方式在菜单上做任何你需要的定制:http://docs.django-cms.org/en/develop/how_to/menus.html
希望对你有所帮助。
以防万一您仍然对此感兴趣,这是我将 Anchor 插件插入菜单的实现:
from menus.base import Modifier, NavigationNode
from menus.menu_pool import menu_pool
from cms.models import Page
from cms.utils.plugins import get_plugins
from cms.templatetags.cms_tags import _get_placeholder
class AnchorPluginMenuModifier(Modifier):
"""
"""
def modify(self, request, nodes, namespace, root_id, post_cut, breadcrumb):
# if the menu is not yet cut, don't do anything
if post_cut:
return nodes
# otherwise loop over the nodes
newnodes = []
for node in nodes:
try:
if "is_page" in node.attr and node.attr["is_page"]:
page_obj = Page.objects.get(id=node.id)
template_context = {
"request" : request,
}
placeholder_name = "content"
placeholder = _get_placeholder(request.current_page, page_obj,
template_context, placeholder_name) # placeholder_name is a string
plugins = get_plugins(request, placeholder, page_obj.get_template())
for plugin in plugins:
if type(plugin).__name__ == "AnchorPluginModel":
newnode = NavigationNode(
plugin.anchor_menutitle,
node.url+"#"+plugin.anchor_name,
"{}-{}".format(page_obj.id,plugin.id),
node
)
newnode.parent_id = node.id
newnodes.append(newnode)
setattr(newnode, "selected", False)
node.children.append(newnode)
except Exception, e:
print e
# client.captureException()
return nodes
menu_pool.register_modifier(AnchorPluginMenuModifier)
代码位于 cms_menus.py 文件中。
我在 django cms 中有一个模板,它基于我添加到内容占位符的插件创建了一个视差网站。这是我的模板:
{% extends "foundry/base.html" %}
{% load cms_tags %}
{% block title %}{% page_attribute "page_title" %}{% endblock title %}
{% block content %}
{% placeholder 'content' %}
{% endblock content %}
在base.html中,我使用{% show_menu 0 1 100 100 "foundry/menu.html" %}来生成菜单。我想根据添加到内容占位符的插件将项目添加到此菜单。因为 show_menu 在 cms 呈现占位符之前调用,所以我无法使用 NavigationNode 来注册我的菜单。如果我可以查询内容占位符中使用的插件,我就可以处理这个菜单。但是 Django CMS 数据库太复杂了,我找不到查询。 谢谢
Django CMS 提供了几个实用程序来执行此操作;你只需要挖掘源代码就可以找到它们。
from cms.templatetags.cms_tags import _get_placeholder
from cms.utils.plugins import get_plugins
if request and request.current_page:
placeholder = _get_placeholder(request.current_page, request.current_page,
template_context, placeholder_name) # placeholder_name is a string
plugins = get_plugins(request, placeholder, request.current_page.get_template())
一旦你有了占位符的插件,你就可以通过以下方式在菜单上做任何你需要的定制:http://docs.django-cms.org/en/develop/how_to/menus.html
希望对你有所帮助。
以防万一您仍然对此感兴趣,这是我将 Anchor 插件插入菜单的实现:
from menus.base import Modifier, NavigationNode
from menus.menu_pool import menu_pool
from cms.models import Page
from cms.utils.plugins import get_plugins
from cms.templatetags.cms_tags import _get_placeholder
class AnchorPluginMenuModifier(Modifier):
"""
"""
def modify(self, request, nodes, namespace, root_id, post_cut, breadcrumb):
# if the menu is not yet cut, don't do anything
if post_cut:
return nodes
# otherwise loop over the nodes
newnodes = []
for node in nodes:
try:
if "is_page" in node.attr and node.attr["is_page"]:
page_obj = Page.objects.get(id=node.id)
template_context = {
"request" : request,
}
placeholder_name = "content"
placeholder = _get_placeholder(request.current_page, page_obj,
template_context, placeholder_name) # placeholder_name is a string
plugins = get_plugins(request, placeholder, page_obj.get_template())
for plugin in plugins:
if type(plugin).__name__ == "AnchorPluginModel":
newnode = NavigationNode(
plugin.anchor_menutitle,
node.url+"#"+plugin.anchor_name,
"{}-{}".format(page_obj.id,plugin.id),
node
)
newnode.parent_id = node.id
newnodes.append(newnode)
setattr(newnode, "selected", False)
node.children.append(newnode)
except Exception, e:
print e
# client.captureException()
return nodes
menu_pool.register_modifier(AnchorPluginMenuModifier)
代码位于 cms_menus.py 文件中。