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


Python NodeList.get_nodes_by_type方法代码示例

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


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

示例1: IncludeNode

# 需要导入模块: from django.template import NodeList [as 别名]
# 或者: from django.template.NodeList import get_nodes_by_type [as 别名]
class IncludeNode(Node):
	def __init__(self, nodelist, path):
		self.template = get_template(path)
		self.nodelist, self.path = NodeList(nodelist), path

	def __repr__(self):
		return "<Include Node: %s Contents: %r>" % (self.path, self.nodelist)
	
	def render(self, context):
		parent_blocks = dict([(n.name, n) for n in self.template.nodelist.get_nodes_by_type(PartNode)])
		for part_node in self.nodelist.get_nodes_by_type(PartNode):
			parent_block = parent_blocks[part_node.name]
			parent_block.nodelist = part_node.nodelist
		return self.template.render(context)
开发者ID:appqui,项目名称:django-tag-include,代码行数:16,代码来源:includetag.py

示例2: ForNode

# 需要导入模块: from django.template import NodeList [as 别名]
# 或者: from django.template.NodeList import get_nodes_by_type [as 别名]
class ForNode(Node):
    def __init__(self, loopvars, sequence, is_reversed, nodelist_loop, nodelist_empty=None):
        self.loopvars, self.sequence = loopvars, sequence
        self.is_reversed = is_reversed
        self.nodelist_loop = nodelist_loop
        if nodelist_empty is None:
            self.nodelist_empty = NodeList()
        else:
            self.nodelist_empty = nodelist_empty

    def __repr__(self):
        reversed_text = self.is_reversed and ' reversed' or ''
        return "<For Node: for %s in %s, tail_len: %d%s>" % \
            (', '.join(self.loopvars), self.sequence, len(self.nodelist_loop),
             reversed_text)

    def __iter__(self):
        for node in self.nodelist_loop:
            yield node
        for node in self.nodelist_empty:
            yield node

    def get_nodes_by_type(self, nodetype):
        nodes = []
        if isinstance(self, nodetype):
            nodes.append(self)
        nodes.extend(self.nodelist_loop.get_nodes_by_type(nodetype))
        nodes.extend(self.nodelist_empty.get_nodes_by_type(nodetype))
        return nodes

    def render(self, context):
        if 'forloop' in context:
            parentloop = context['forloop']
        else:
            parentloop = {}
        context.push()
        try:
            values = self.sequence.resolve(context, True)
        except VariableDoesNotExist:
            values = []
        if values is None:
            values = []
        if not hasattr(values, '__len__'):
            values = list(values)
        len_values = len(values)
        if len_values < 1:
            context.pop()
            return self.nodelist_empty.render(context)
        nodelist = NodeList()
        if self.is_reversed:
            values = reversed(values)
        unpack = len(self.loopvars) > 1
        # Create a forloop value in the context.  We'll update counters on each
        # iteration just below.
        loop_dict = context['forloop'] = {'parentloop': parentloop}
        for i, item in enumerate(values):
            # Shortcuts for current loop iteration number.
            loop_dict['counter0'] = i
            loop_dict['counter'] = i+1
            # Reverse counter iteration numbers.
            loop_dict['revcounter'] = len_values - i
            loop_dict['revcounter0'] = len_values - i - 1
            # Boolean values designating first and last times through loop.
            loop_dict['first'] = (i == 0)
            loop_dict['last'] = (i == len_values - 1)

            if unpack:
                # If there are multiple loop variables, unpack the item into
                # them.
                context.update(dict(zip(self.loopvars, item)))
            else:
                context[self.loopvars[0]] = item
            for node in self.nodelist_loop:
                nodelist.append(node.render(context))
            if unpack:
                # The loop variables were pushed on to the context so pop them
                # off again. This is necessary because the tag lets the length
                # of loopvars differ to the length of each set of items and we
                # don't want to leave any vars from the previous loop on the
                # context.
                context.pop()
        context.pop()
        return nodelist.render(context)
开发者ID:AloneRoad,项目名称:Inforlearn,代码行数:85,代码来源:defaulttags.py


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