本文整理汇总了Python中django.template.NodeList.extend方法的典型用法代码示例。如果您正苦于以下问题:Python NodeList.extend方法的具体用法?Python NodeList.extend怎么用?Python NodeList.extend使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.template.NodeList
的用法示例。
在下文中一共展示了NodeList.extend方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: labjs
# 需要导入模块: from django.template import NodeList [as 别名]
# 或者: from django.template.NodeList import extend [as 别名]
def labjs(parser, token):
"""
Renders a labjs queue from linked js.
Syntax::
{% labjs %}
<html of linked JS>
{% endlabjs %}
Examples::
{% labjs %}
<script
type="text/javascript"
src="{{ STATIC_URL }}js/jquery-1.5.2.min.js">
</script>
{% wait %}
<script
type="text/javascript"
src="{{ STATIC_URL }}js/jquery.formset.min.js">
</script>
<script
type="text/javascript"
src="{% url django.views.i18n.javascript_catalog %}">
</script>
{% endlabjs %}
Which would be rendered something like::
<script type="text/javascript">
$LAB.queueScript("/static/js/jquery-1.5.2.min.js")
.queueScript("/static/js/jquery.formset.min.js")
.queueScript("/jsi18n/");
</script>
"""
nodelist = NodeList()
while True:
chunk = parser.parse(('endlabjs', 'wait'))
ptoken = parser.next_token()
if ptoken.contents == 'wait':
chunk.append(Wait())
nodelist.extend(chunk)
elif ptoken.contents == 'endlabjs':
nodelist.extend(chunk)
break
return LabjsNode(nodelist)
示例2: IfBlockNode
# 需要导入模块: from django.template import NodeList [as 别名]
# 或者: from django.template.NodeList import extend [as 别名]
class IfBlockNode(BlockNode):
def __init__(self, name, nodelist, parent=None, check_node=None):
self.name, self.nodelist, self.parent = name, nodelist, parent
self.original_nodelist = NodeList()
self.final_nodelist = NodeList()
self.original_nodelist.extend( self.nodelist )
self.final_nodelist.extend( self.nodelist )
self.check_node = check_node
self.index = None
for i, node in enumerate( nodelist ):
if isinstance(node, self.check_node.__class__):
if node.filter_expression.token == self.check_node.filter_expression.token:
self.index = i
break
def __repr__(self):
return "<MyBlock Node: %s. Contents: %r>" % (self.name, self.nodelist)
def render(self, context):
result = ''
if self.parent:
p = self
pre = []
post = []
while p.parent:
p = p.parent
if p.nodelist == self.original_nodelist:
p.nodelist = NodeList()
else:
index = None
found = False
for i, node in enumerate( p.nodelist ):
if isinstance(node, self.check_node.__class__):
if node.filter_expression.token == self.check_node.filter_expression.token:
index = i
found = True
if found:
pre[0:0] = p.nodelist[:index]
post.extend(p.nodelist[index+1:])
p.nodelist = NodeList()
if pre:
self.final_nodelist[self.index:self.index] = pre
self.index += len(pre)
if post:
self.final_nodelist[self.index+1:self.index+1] = post
context.push()
# Save context in case of block.super().
self.context = context
context['block'] = self
pre_result = self.nodelist.render(context)
context['blockoutput'] = pre_result
result = self.final_nodelist.render(context)
context.pop()
return result
def super(self):
if self.parent:
return mark_safe(self.parent.render(self.context))
return ''
def add_parent(self, nodelist):
if self.parent:
self.parent.add_parent(nodelist)
else:
self.parent = BlockNode(self.name, nodelist)