当前位置: 首页>>代码示例>>Python>>正文


Python Context.bind_template方法代码示例

本文整理汇总了Python中django.template.context.Context.bind_template方法的典型用法代码示例。如果您正苦于以下问题:Python Context.bind_template方法的具体用法?Python Context.bind_template怎么用?Python Context.bind_template使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.template.context.Context的用法示例。


在下文中一共展示了Context.bind_template方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: get_all_templates_used

# 需要导入模块: from django.template.context import Context [as 别名]
# 或者: from django.template.context.Context import bind_template [as 别名]
def get_all_templates_used(nodelist, current_block=None, ignore_blocks=None):
    if ignore_blocks is None:
        ignore_blocks = []

    found = []
    for node in nodelist:
        if isinstance(node, IncludeNode) and node.template:

            # This is required for Django 1.7 but works on older version too
            # Check if it quacks like a template object, if not
            # presume is a template path and get the object out of it
            if not callable(getattr(node.template, 'render', None)):
                # If it's a variable there is no way to expand it at this stage so we
                # need to skip it
                if isinstance(node.template.var, Variable):
                    continue
                else:
                    template = get_template(node.template.var)
            else:
                template = node.template
            if not hasattr(template, 'name'):
                template = template.template
            found.append(template.name)
            found += get_all_templates_used(_get_nodelist(template))
        elif isinstance(node, ExtendsNode):
            template = node.get_parent(FAKE_CONTEXT)
            if not hasattr(template, 'name'):
                template = template.template
            found.append(template.name)
            found += _extend_nodelist(node)
            if hasattr(node, 'child_nodelists'):
                for child_lst in node.child_nodelists:
                    _found_to_add, current_block = _scan_nodelist(
                        getattr(node, child_lst, ''), node, current_block)
                    found += _found_to_add
        elif isinstance(node, RenderBlock):
            template = Template('')
            context = Context()
            with context.bind_template(template):
                node.kwargs['name'].resolve(context)
            found += get_all_templates_used(node.blocks['nodelist'], node)
        elif (isinstance(node, VariableNode) and current_block and
              node.filter_expression.token == 'block.super' and
              hasattr(current_block.super, 'nodelist')):
            found += get_all_templates_used(
                _get_nodelist(current_block.super), current_block.super)
        elif isinstance(node, BlockNode) and node.name in ignore_blocks:
            continue
        elif (isinstance(node, ShowMenu) or isinstance(node, ShowSubMenu) or
              isinstance(node, ShowBreadcrumb)):
            menu_template_node = node.kwargs.get('template', None)
            if menu_template_node and hasattr(menu_template_node, 'var'):
                template = Template('')
                context = Context()
                with context.bind_template(template):
                    menu_template_name = menu_template_node.var.resolve(context)

                if menu_template_name:
                    found.append(menu_template_name)
                    compiled_template = get_template(menu_template_name)
                    found += get_all_templates_used(_get_nodelist(compiled_template))
        elif hasattr(node, 'child_nodelists'):
            for child_lst in node.child_nodelists:
                _found_to_add, current_block = _scan_nodelist(
                    getattr(node, child_lst, ''), node, current_block)
                found += _found_to_add
        else:
            for attr in dir(node):
                _found_to_add, current_block = _scan_nodelist(
                    getattr(node, attr), node, current_block)
                found += _found_to_add
    return found
开发者ID:pbs,项目名称:django-cms-dbtemplates,代码行数:74,代码来源:template_analyzer.py


注:本文中的django.template.context.Context.bind_template方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。