本文整理汇总了Python中django.template.base.NodeList.append方法的典型用法代码示例。如果您正苦于以下问题:Python NodeList.append方法的具体用法?Python NodeList.append怎么用?Python NodeList.append使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.template.base.NodeList
的用法示例。
在下文中一共展示了NodeList.append方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: render
# 需要导入模块: from django.template.base import NodeList [as 别名]
# 或者: from django.template.base.NodeList import append [as 别名]
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)
示例2: remove_block_nodes
# 需要导入模块: from django.template.base import NodeList [as 别名]
# 或者: from django.template.base.NodeList import append [as 别名]
def remove_block_nodes(nodelist, block_stack, block_context):
new_nodelist = NodeList()
for node in nodelist:
if isinstance(node, VariableNode):
var_name = node.filter_expression.token.strip()
if var_name == 'block.super':
if not block_stack:
continue
node = block_context.get_block(block_stack[-1].name)
if not node:
continue
if isinstance(node, BlockNode):
expanded_block = expand_blocknode(node, block_stack, block_context)
new_nodelist.extend(expanded_block)
else:
# IfNode has nodelist as a @property so we can not modify it
if isinstance(node, IfNode):
node = copy(node)
for i, (condition, sub_nodelist) in enumerate(node.conditions_nodelists):
sub_nodelist = remove_block_nodes(sub_nodelist, block_stack, block_context)
node.conditions_nodelists[i] = (condition, sub_nodelist)
else:
for attr in node.child_nodelists:
sub_nodelist = getattr(node, attr, None)
if sub_nodelist:
sub_nodelist = remove_block_nodes(sub_nodelist, block_stack, block_context)
node = copy(node)
setattr(node, attr, sub_nodelist)
new_nodelist.append(node)
return new_nodelist
示例3: do_random
# 需要导入模块: from django.template.base import NodeList [as 别名]
# 或者: from django.template.base.NodeList import append [as 别名]
def do_random(parser, token):
"""
Output the contents of a random block.
The `random` block tag must contain one or more `or` tags, which separate
possible choices; a choice in this context is everything between a
`random` and `or` tag, between two `or` tags, or between an `or` and an
`endrandom` tag.
Sample usage::
{% random %}
You will see me half the time.
{% or %}
You will see <em>me</em> the other half.
{% endrandom %}
"""
options = NodeList()
while True:
option = parser.parse(('or', 'endrandom'))
token = parser.next_token()
options.append(option)
if token.contents == 'or':
continue
parser.delete_first_token()
break
if len(options) < 2:
raise TemplateSyntaxError
return RandomNode(options)
示例4: angularjs
# 需要导入模块: from django.template.base import NodeList [as 别名]
# 或者: from django.template.base.NodeList import append [as 别名]
def angularjs(parser, token):
"""
Conditionally switch between AngularJS and Django variable expansion.
Usage::
{% angularjs 1 %} or simply {% angularjs %}
{% process variables through the AngularJS template engine %}
{% endangularjs %}
{% angularjs 0 %}
{% process variables through the Django template engine %}
{% endangularjs %}
Instead of 0 and 1, it is possible to use a context variable.
"""
bits = token.contents.split()
if len(bits) < 2:
bits.append('1')
values = [parser.compile_filter(bit) for bit in bits[1:]]
django_nodelist = parser.parse(('endangularjs',))
angular_nodelist = NodeList()
for node in django_nodelist:
# convert all occurrences of VariableNode into a TextNode using the
# AngularJS double curly bracket notation
if isinstance(node, VariableNode):
node = TextNode('{{ %s }}' % node.filter_expression.token)
angular_nodelist.append(node)
parser.delete_first_token()
return AngularJsNode(django_nodelist, angular_nodelist, values[0])
示例5: render
# 需要导入模块: from django.template.base import NodeList [as 别名]
# 或者: from django.template.base.NodeList import append [as 别名]
def render(self, context):
permissions = [permission.resolve(context, True) for permission in self.permissions]
#
# check if has permissions
#
from core.helper import get_request
from core.permission import check_permissions
status, action = check_permissions(get_request(), permissions)
nodelist = NodeList()
if status:
for node in self.nodelist_permission:
try:
nodelist.append(node.render(context))
except Exception as e:
if not hasattr(e, 'django_template_source'):
e.django_template_source = node.source
raise
else:
if self.nodelist_nopermission:
for node in self.nodelist_nopermission:
try:
nodelist.append(node.render(context))
except Exception as e:
if not hasattr(e, 'django_template_source'):
e.django_template_source = node.source
raise
return nodelist.render(context)
示例6: render
# 需要导入模块: from django.template.base import NodeList [as 别名]
# 或者: from django.template.base.NodeList import append [as 别名]
def render(self, context):
context.push();
kwargs = self.resolve_kwargs(context)
items = Item.objects(type=kwargs['type']).order_by('-itemMeta.versionCreated')[:kwargs['length']]
nodelist = NodeList()
for item in items:
context['item'] = item
for node in self.nodelist:
nodelist.append(node.render(context))
context.pop()
return nodelist.render(context)
示例7: render
# 需要导入模块: from django.template.base import NodeList [as 别名]
# 或者: from django.template.base.NodeList import append [as 别名]
def render(self, context):
context.push()
nodelist = NodeList()
kwargs = self.resolve_kwargs(context)
limit = kwargs.get('limit', 55)
start = kwargs.get('start', 0)
order = kwargs.get('order', '-versionCreated')
items = Item.objects(itemClass=kwargs['class'],publishedOn__ne=None).order_by(order)[start:limit]
for item in items:
context['item'] = item
for node in self.nodelist:
nodelist.append(node.render(context))
context.pop()
return nodelist.render(context)