本文整理汇总了Python中django.template.NodeList.render方法的典型用法代码示例。如果您正苦于以下问题:Python NodeList.render方法的具体用法?Python NodeList.render怎么用?Python NodeList.render使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.template.NodeList
的用法示例。
在下文中一共展示了NodeList.render方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: render
# 需要导入模块: from django.template import NodeList [as 别名]
# 或者: from django.template.NodeList import render [as 别名]
def render(self, context):
nodelist = NodeList()
if context.has_key('parenttable'):
parenttable = context['parenttable']
else:
parenttable = {}
context.push()
values = self.get_values(context)
len_values = len(values)
innernodelist = NodeList()
totalrows = int(math.ceil(float(len_values) / float(self.cols)))
rowcount = 0
for i, item in enumerate(values):
loopctx = {
'counter0':i,
'counter':i+1,
'rowcounter0':(i/self.cols),
'rowcounter':((i/self.cols)+1),
'firstrow':(i<self.cols),
'lastrow':(i>len_values-self.cols),
'firstcell':(i==0),
'lastcell':(i==len_values-1),
'evencol':(i%2)==0,
'oddcol':(i%2)==1,
'parenttable':parenttable,
}
context[self.cellvar] = item
loopctx['oddrow'] = False
loopctx['evenrow'] = False
loopctx['lastcellinrow'] = False
loopctx["startrow"] = False
loopctx["endrow"] = loopctx["lastcell"]
if totalrows == 1 and i == len_values-1:
loopctx['lastcellinrow'] = True
elif i == (len_values-1):
loopctx['lastcellinrow'] = True
if i % self.cols == 0:
nodelist.append(innernodelist.render(context))
innernodelist = NodeList()
loopctx["startrow"] = True
if (rowcount+1)%2==0:
loopctx["oddrow"] = False
loopctx["evenrow"] = True
else:
loopctx["oddrow"] = True
loopctx["evenrow"] = False
elif (i+1) % self.cols==0:
loopctx['lastcellinrow'] = True
loopctx["endrow"] = True
rowcount += 1
context['table'] = loopctx
for node in self.cellnodes:
innernodelist.append(node.render(context))
if innernodelist and len(innernodelist)>0:
nodelist.append(innernodelist.render(context))
context.pop()
return nodelist.render(context)
示例2: ThumbnailNode
# 需要导入模块: from django.template import NodeList [as 别名]
# 或者: from django.template.NodeList import render [as 别名]
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
示例3: render
# 需要导入模块: from django.template import NodeList [as 别名]
# 或者: from django.template.NodeList import render [as 别名]
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)
示例4: render
# 需要导入模块: from django.template import NodeList [as 别名]
# 或者: from django.template.NodeList import render [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)
示例5: render
# 需要导入模块: from django.template import NodeList [as 别名]
# 或者: from django.template.NodeList import render [as 别名]
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)
示例6: render
# 需要导入模块: from django.template import NodeList [as 别名]
# 或者: from django.template.NodeList import render [as 别名]
def render(self, context):
actual_form = self.form_var.resolve(context)
bindings = get_bindings(actual_form)
# bindings.reverse() # bind in reverse order ...
nodelist = NodeList()
js_info = get_js_info(actual_form)
# Add the script to activate this form
nodelist.append(
HtmlContent(
"""
<script>
%s
$(document).ready(function(){
// new ManagementForm(parentForm, childTemplate, initialForms);
%s
});
</script>
"""
% (js_info, "\n".join(bindings))
)
)
nodelist.append(HtmlContent("</div>"))
return nodelist.render(context)
示例7: render
# 需要导入模块: from django.template import NodeList [as 别名]
# 或者: from django.template.NodeList import render [as 别名]
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)
示例8: render
# 需要导入模块: from django.template import NodeList [as 别名]
# 或者: from django.template.NodeList import render [as 别名]
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 ""
示例9: do_render
# 需要导入模块: from django.template import NodeList [as 别名]
# 或者: from django.template.NodeList import render [as 别名]
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)
示例10: render
# 需要导入模块: from django.template import NodeList [as 别名]
# 或者: from django.template.NodeList import render [as 别名]
def render(self, context, depth=0, values=False):
nodelist = NodeList()
if 'recurseloop' in context:
parentloop = context['recurseloop']
else:
parentloop = {}
context.push()
# On the first recursion pass, we have no values
if not values:
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)
# Create a recurseloop value in the context. We'll update counters on each iteration just below.
loop_dict = context['recurseloop'] = {'parent': parentloop}
loop_dict['depth'] = depth + 1
loop_dict['depth0'] = depth
for i, item in enumerate(values):
# Add the additional arguments to the context
# They come in the form of {'name':(initial,increment)}
# As for now only numbers are supported, but also strings can be multiplied
for k,v in self.kwargs.iteritems():
context[k] = v[0] + v[1]*depth
# Shortcuts for current loop iteration number.
loop_dict['counter0'] = i
loop_dict['counter'] = i+1
# Boolean values designating first and last times through loop.
loop_dict['first'] = (i == 0)
loop_dict['last'] = (i == len_values - 1)
context[ self.loopvar ] = item
for node in self.nodelist_first:
nodelist.append( node.render(context) )
if len( getattr( item, self.children_name ) ):
nodelist.append( self.render( context, depth+1, getattr( item, self.children_name ) ) )
for node in self.nodelist_second:
nodelist.append( node.render(context) )
context.pop()
return nodelist.render(context)
示例11: render_mixers
# 需要导入模块: from django.template import NodeList [as 别名]
# 或者: from django.template.NodeList import render [as 别名]
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)
示例12: render
# 需要导入模块: from django.template import NodeList [as 别名]
# 或者: from django.template.NodeList import render [as 别名]
def render(self, context):
content_type_id = self.content_type_id.resolve(context)
content_type = ContentType.objects.get_for_id(content_type_id)
if hasattr(content_type.model_class(), 'get_absolute_url'):
pk = self.pk.resolve(context)
nodelist = NodeList()
#nodelist.append(LINK_FORMAT % (content_type.pk, pk))
nodelist.append(self.inner_nodelist.render(context))
nodelist.append('</a>')
return nodelist.render(context)
else:
return self.inner_nodelist.render(context)
示例13: render
# 需要导入模块: from django.template import NodeList [as 别名]
# 或者: from django.template.NodeList import render [as 别名]
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
# 需要导入模块: from django.template import NodeList [as 别名]
# 或者: from django.template.NodeList import render [as 别名]
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
# 需要导入模块: from django.template import NodeList [as 别名]
# 或者: from django.template.NodeList import render [as 别名]
class ThumbnailNode(thumbnail.ThumbnailNodeBase):
child_nodelists = ("nodelist_file", "nodelist_empty")
error_msg = "Syntax error. Expected: ``thumbnail source preset as var``"
preset_error = "Preset %s not found."
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.preset = parser.compile_filter(bits[2])
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)
preset_name = self.preset.resolve(context)
preset = Preset.objects.get(name=preset_name)
if settings.THUMBNAIL_DUMMY:
thumbnail = DummyImageFile(preset.geometry)
elif file_:
thumbnail = default.backend.get_thumbnail(file_, preset.geometry, **preset.get_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 "<ImageCacheNode>"
def __iter__(self):
for node in self.nodelist_file:
yield node
for node in self.nodelist_empty:
yield node