mezzanine的head 导航条、左侧tree、footer是由page_menu产生的。page_menu的算法,先计算出每一页的孩子,然后再逐页去page_menu。

@register.render_tag
def page_menu(context, token):
"""
Return a list of child pages for the given parent, storing all
pages in a dict in the context when first called using parents as keys
for retrieval on subsequent recursive calls from the menu template.
"""
# First arg could be the menu template file name, or the parent page.
# Also allow for both to be used.
template_name = None
parent_page = None
parts = token.split_contents()[1:]#对page_menu菜单分离,取得参数
for part in parts:
part = Variable(part).resolve(context)
if isinstance(part, str):
template_name = part#文本为模板名
elif isinstance(part, Page):
parent_page = part#父页
if template_name is None:#没有提供模板的话,从上下文中获取
try:
template_name = context["menu_template_name"]
except KeyError:
error = "No template found for page_menu in: %s" % parts
raise TemplateSyntaxError(error)
context["menu_template_name"] = template_name#设置模板
if "menu_pages" not in context:#没有菜单页,取得当前用户有权访问的页
try:
user = context["request"].user
slug = context["request"].path
except KeyError:
user = None
slug = ""
num_children = lambda id: lambda: len(context["menu_pages"][id])#匿名函数,函数中包含函数
has_children = lambda id: lambda: num_children(id)() > 0
rel = [m.__name__.lower()#取得Page的子类,但不包括Page自身
for m in Page.get_content_models()
if not m._meta.proxy]
published = Page.objects.published(for_user=user).select_related(*rel)#把相关联的model也进行查询
# Store the current page being viewed in the context. Used
# for comparisons in page.set_menu_helpers.
if "page" not in context:
try:
context.dicts[0]["_current_page"] = published.exclude(
content_model="link").get(slug=slug)
except Page.DoesNotExist:
context.dicts[0]["_current_page"] = None
elif slug:
context.dicts[0]["_current_page"] = context["page"]#记录当前页
# Some homepage related context flags. on_home is just a helper
# indicated we're on the homepage. has_home indicates an actual
# page object exists for the homepage, which can be used to
# determine whether or not to show a hard-coded homepage link
# in the page menu.
home = home_slug()
context.dicts[0]["on_home"] = slug == home
context.dicts[0]["has_home"] = False
# Maintain a dict of page IDs -> parent IDs for fast
# lookup in setting page.is_current_or_ascendant in
# page.set_menu_helpers.
context.dicts[0]["_parent_page_ids"] = {}#保存父页id的词典
pages = defaultdict(list)#pages[]不存在时,不会出错,返回[]
for page in published.order_by("_order"):
page.set_helpers(context)#判断是否是当前页,有无孩子
context["_parent_page_ids"][page.id] = page.parent_id#各页的父
setattr(page, "num_children", num_children(page.id))
setattr(page, "has_children", has_children(page.id))
pages[page.parent_id].append(page)#取得各页的子页list
if page.slug == home:
context.dicts[0]["has_home"] = True
# Include menu_pages in all contexts, not only in the
# block being rendered.
context.dicts[0]["menu_pages"] = pages
# ``branch_level`` must be stored against each page so that the
# calculation of it is correctly applied. This looks weird but if we do
# the ``branch_level`` as a separate arg to the template tag with the
# addition performed on it, the addition occurs each time the template
# tag is called rather than once per level.
context["branch_level"] = 0#默认层次为0
parent_page_id = None
if parent_page is not None:
context["branch_level"] = getattr(parent_page, "branch_level", 0) + 1
parent_page_id = parent_page.id # Build the ``page_branch`` template variable, which is the list of
# pages for the current parent. Here we also assign the attributes
# to the page object that determines whether it belongs in the
# current menu template being rendered.
context["page_branch"] = context["menu_pages"].get(parent_page_id, [])#取得该页的子页
context["page_branch_in_menu"] = False
for page in context["page_branch"]:#处理该分支
page.in_menu = page.in_menu_template(template_name)#判断是否在该菜单
page.num_children_in_menu = 0
if page.in_menu:
context["page_branch_in_menu"] = True
for child in context["menu_pages"].get(page.id, []):
if child.in_menu_template(template_name):
page.num_children_in_menu += 1
page.has_children_in_menu = page.num_children_in_menu > 0
page.branch_level = context["branch_level"]
page.parent = parent_page
context["parent_page"] = page.parent # Prior to pages having the ``in_menus`` field, pages had two
# boolean fields ``in_navigation`` and ``in_footer`` for
# controlling menu inclusion. Attributes and variables
# simulating these are maintained here for backwards
# compatibility in templates, but will be removed eventually.
page.in_navigation = page.in_menu
page.in_footer = not (not page.in_menu and "footer" in template_name)
if page.in_navigation:
context["page_branch_in_navigation"] = True
if page.in_footer:
context["page_branch_in_footer"] = True t = get_template(template_name)
return t.render(Context(context))

mezzanine的page_menu tag的更多相关文章

  1. mezzanine的page_menu tag(二)

    dict的特性,key可以是None >>> def f(): a=[2,3] return a #函数返回local变量 >>> a=f() >>&g ...

  2. mezzanine的breadcrumb

    page_menu tag为什么能渲染出当前页的面包屑路径??靠的是request的page对象,要是page的自己或是它的的祖先才渲染,是根据page的属性page.is_current_or_as ...

  3. mezzanine的page表

    class Orderable(with_metaclass(OrderableBase, models.Model)): """ Abstract model that ...

  4. Git 进阶指南(git ssh keys / reset / rebase / alias / tag / submodule )

    在掌握了基础的 Git 使用 之后,可能会遇到一些常见的问题.以下是猫哥筛选总结的部分常见问题,分享给各位朋友,掌握了这些问题的中的要点之后,git 进阶也就完成了,它包含以下部分: 如何修改 ori ...

  5. 05.GitHub实战系列~5.发布版本之分支操作+Tag讲解 2015-12-14

    GitHub实战系列汇总:http://www.cnblogs.com/dunitian/p/5038719.html ———————————————————————————————————————— ...

  6. Encountered an unexpected error when attempting to resolve tag helper directive '@addTagHelper' with value '"*, Microsoft.AspNet.Mvc.TagHelpers"'

    project.json 配置: { "version": "1.0.0-*", "compilationOptions": { " ...

  7. ASP.NET Core 中文文档 第四章 MVC(3.6.1 )Tag Helpers 介绍

    原文:Introduction to Tag Helpers 作者:Rick Anderson 翻译:刘浩杨 校对:高嵩(Jack) 什么是 Tag Helpers? Tag Helpers 提供了什 ...

  8. ASP.NET Core 中文文档 第四章 MVC(3.6.2 )自定义标签辅助类(Tag Helpers)

    原文:Authoring Tag Helpers 作者:Rick Anderson 翻译:张海龙(jiechen) 校对:许登洋(Seay) 示例代码查看与下载 从 Tag Helper 讲起 本篇教 ...

  9. VS2015突然报错————Encountered an unexpected error when attempting to resolve tag helper directive '@addTagHelper' with value 'Microsoft.AspNet.Mvc.Razor.TagHelpers.UrlResolutionTagHelper

    Encountered an unexpected error when attempting to resolve tag helper directive '@addTagHelper' with ...

随机推荐

  1. JAVA面向对象编程之购物车界面的设计与功能的实现

    1.小组成员及分工 小组成员 负责工作 兰泽祥 负责总体界面.Git代码库 吴修恩 负责MVC模式.类名包名的规范化 2.Git 仓库地址 仓库地址:https://gitee.com/lanzexi ...

  2. 安装node.msi 格式的文件失败

    in10 安装node.msi格式的文件,出现2503 2502 错误码, win+x 打开 在命令提示符窗口中输入: msiexec /package "安装msi格式的文件的全路径&qu ...

  3. Python Selenium set Chrome Preference Download Location.

    def set_chrome_pref(self): chromeOptions = webdriver.ChromeOptions() prefs = {"download.default ...

  4. pytest.3.Assert

    From: http://www.testclass.net/pytest/assert/ Assert就是断言,每个测试用例都需要断言. 与unittest不同,pytest使用的是python自带 ...

  5. 通过分析Ajax请求 抓取今日头条街拍图集

    代码: import os import re import json import time from hashlib import md5 from multiprocessing import ...

  6. SQL优化系列——查询优化器

    大多数查询优化器将查询计划用“计划节点”树表示.计划节点封装执行查询所需的单个操作.节点被布置为树,中间结果从树的底部流向顶部.每个节点具有零个或多个子节点 - 这些子节点是输出作为父节点输入的节点. ...

  7. PAT 乙级 1077 互评成绩计算 (20)

    在浙大的计算机专业课中,经常有互评分组报告这个环节.一个组上台介绍自己的工作,其他组在台下为其表现评分.最后这个组的互评成绩是这样计算的:所有其他组的评分中,去掉一个最高分和一个最低分,剩下的分数取平 ...

  8. 基于Html的SEO(很基础,更是前端必须掌握之点)

    文章来源:百度文库   Html代码seo优化最佳布局,实例讲解     众所周知,搜索引擎对html代码是非常优化的,所以html的优化是做好推广的第一步.一个符合seo规则的代码大体如下界面所示. ...

  9. version control(关于版本控制)

    版本控制(Version Control Systems)是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统.  这个系统可以自动帮我们备份文件的每一次更改,并且可以非常方便的恢复到 ...

  10. Hibernate QBC

    QBC查询: Query By Criteria 使用面向对象的方式查询 和HQL单表查询相似,但不包含别名查询和具名查询   1 全表查询 Criteria ce = session.createC ...