本文整理汇总了Python中django.template.NodeList类的典型用法代码示例。如果您正苦于以下问题:Python NodeList类的具体用法?Python NodeList怎么用?Python NodeList使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NodeList类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_render_one_element
def test_render_one_element(self):
model = generate_random_model()
nodelist = NodeList()
nodelist.append(layout_helpers.RenderNextNode("'full_page'"))
node = layout_helpers.RenderIterNode(Variable("list"), nodelist)
rendered = node.render(Context({"list": [model]}))
self.assertTrue(re.search(model.title, rendered))
示例2: render
def render(self, context):
nodelist = NodeList()
context.push()
try:
start = self.start.resolve(context)
except VariableDoesNotExist:
return ''
except AttributeError:
start = self.start
try:
end = self.end.resolve(context)
except VariableDoesNotExist:
return ''
except AttributeError:
end = self.end
try:
step = self.step.resolve(context)
except VariableDoesNotExist:
return ''
except AttributeError:
step = self.step
for i in xrange(start, end+1, step):
context[self.var_name] = i
for node in self.nodelist_loop:
nodelist.append(node.render(context))
context.pop()
return nodelist.render(context)
示例3: render
def render (self, context):
nodelist = NodeList ()
for i in range (int(self.value)):
context['percentage'] = {'iter': i}
for node in self.nodelist_loop:
nodelist.append (node.render(context))
return nodelist.render (context)
示例4: render
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)
pop_context = False
if unpack:
# If there are multiple loop variables, unpack the item into
# them.
try:
unpacked_vars = dict(zip(self.loopvars, item))
except TypeError:
pass
else:
pop_context = True
context.update(unpacked_vars)
else:
context[self.loopvars[0]] = item
for node in self.nodelist_loop:
nodelist.append(node.render(context))
if pop_context:
# 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)
示例5: test_render_non_iterable
def test_render_non_iterable(self):
model = generate_random_model()
nodelist = NodeList()
nodelist.append(layout_helpers.RenderNextNode("'full_page'"))
node = layout_helpers.RenderIterNode(Variable("list"), nodelist)
with self.assertRaises(TypeError):
rendered = node.render(Context({"list": model}))
示例6: render
def render(self, context):
#resolve cube from context
try:
cube = self.cube.resolve(context, False)
except VariableDoesNotExist:
return ''
#resolve dimensions
dimensions = []
for dimension in self.dimensions:
matched = re.match('(?P<quote>"|\')(?P<literal>\w+)(?P=quote)', dimension)
if matched:
dimensions.append(str(matched.group('literal')))
else:
try:
dimensions.append(str(Variable(dimension).resolve(context)))
except VariableDoesNotExist:
return ''
#loop subcubes and render nodes
nodelist = NodeList()
for subcube in cube.subcubes(*dimensions):
context[self.subcube_var] = subcube
for node in self.nodelist:
nodelist.append(node.render(context))
return nodelist.render(context)
示例7: render
def render(self, context):
if (type(self.value) == StringType):
context[self.key] = self.value
else:
nodelist = NodeList()
for node in self.value:
nodelist.append(node.render(context))
context[self.key] = nodelist.render(context)
return ""
示例8: do_render
def do_render(self, context, sequence):
nodelist = NodeList()
context.push()
for item in sequence:
context[self.repeatvar.token] = item
for node in self.nodelist:
nodelist.append(node.render(context))
context.pop()
return nodelist.render(context)
示例9: render_mixers
def render_mixers(self, context):
nodelist = NodeList()
inner_nodelist = self.inner_nodelist
context.push()
for mixer in self.mixers:
context['mixer'] = mixer.template.render(context)
nodelist.append(inner_nodelist.render(context))
context.pop()
return nodelist.render(context)
示例10: labjs
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)
示例11: test_render_multiple_elements_with_extra_nexts
def test_render_multiple_elements_with_extra_nexts(self):
models = [generate_random_model() for i in range(2)]
nodelist = NodeList()
nodelist.append(layout_helpers.RenderNextNode("'full_page'"))
nodelist.append(layout_helpers.RenderNextNode("'full_page'"))
nodelist.append(layout_helpers.RenderNextNode("'show_request'"))
nodelist.append(layout_helpers.RenderNextNode("'show_request'"))
node = layout_helpers.RenderIterNode(Variable("list"), nodelist)
rendered = node.render(Context({"list": models}))
self.assertTrue(re.search(models[0].title, rendered))
self.assertTrue(re.search(models[1].title, rendered))
self.assertFalse(re.search('request', rendered))
示例12: __init__
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
示例13: render
def render(self, context):
nodelist = NodeList()
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 self.reversed:
values = reversed(values)
unpack = len(self.loopvars) > 1
for i, item in enumerate(values):
context['forloop'] = {
# Shortcuts for current loop iteration number.
'counter0': i,
'counter': i+1,
# Reverse counter iteration numbers.
'revcounter': len_values - i,
'revcounter0': len_values - i - 1,
# Boolean values designating first and last times through loop.
'first': (i == 0),
'last': (i == len_values - 1),
'parentloop': parentloop,
}
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)
示例14: render
def render(self, context):
nodelist = NodeList()
sidebar_list = Sidebar.all().order("order").fetch(100)
context.push()
for sidebar in sidebar_list:
context.push()
context["sidebar"] = sidebar
for node in self.nodelist_loop:
nodelist.append(node.render(context))
context.pop()
context.pop()
return nodelist.render(context)
示例15: ThumbnailNode
class ThumbnailNode(ThumbnailNodeBase):
child_nodelists = ('nodelist_file', 'nodelist_empty')
error_msg = ('Syntax error. Expected: ``thumbnail source geometry '
'[key1=val1 key2=val2...] as var``')
def __init__(self, parser, token):
bits = token.split_contents()
if len(bits) < 5 or bits[-2] != 'as':
raise TemplateSyntaxError(self.error_msg)
self.file_ = parser.compile_filter(bits[1])
self.geometry = parser.compile_filter(bits[2])
self.options = []
for bit in bits[3:-2]:
m = kw_pat.match(bit)
if not m:
raise TemplateSyntaxError(self.error_msg)
key = smart_str(m.group('key'))
expr = parser.compile_filter(m.group('value'))
self.options.append((key, expr))
self.as_var = bits[-1]
self.nodelist_file = parser.parse(('empty', 'endthumbnail',))
if parser.next_token().contents == 'empty':
self.nodelist_empty = parser.parse(('endthumbnail',))
parser.delete_first_token()
else:
self.nodelist_empty = NodeList()
def _render(self, context):
file_ = self.file_.resolve(context)
geometry = self.geometry.resolve(context)
options = {}
for key, expr in self.options:
noresolve = {'True': True, 'False': False, 'None': None}
value = noresolve.get(unicode(expr), expr.resolve(context))
if key == 'options':
options.update(value)
else:
options[key] = value
if settings.THUMBNAIL_DUMMY:
thumbnail = DummyImageFile(geometry)
elif file_:
thumbnail = default.backend.get_thumbnail(
file_, geometry, **options
)
else:
return self.nodelist_empty.render(context)
context.push()
context[self.as_var] = thumbnail
output = self.nodelist_file.render(context)
context.pop()
return output
def __repr__(self):
return "<ThumbnailNode>"
def __iter__(self):
for node in self.nodelist_file:
yield node
for node in self.nodelist_empty:
yield node