本文整理汇总了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)
示例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)