class BlockNode
from django.template.loader_tags import BlockNode
Ancestors (MRO)
Attribute | Type | Defined in |
---|---|---|
__dict__ |
getset_descriptor
|
django.template.base.Node |
__weakref__ |
getset_descriptor
|
django.template.base.Node |
Attribute | Value | Defined in |
---|---|---|
child_nodelists |
('nodelist',) |
django.template.base.Node |
must_be_first |
False |
django.template.base.Node |
token |
None |
django.template.base.Node |
def __init__(self, name, nodelist, parent=None)
django.template.loader_tags.BlockNode
Initialize self. See help(type(self)) for accurate signature.
def __init__(self, name, nodelist, parent=None):
self.name = name
self.nodelist = nodelist
self.parent = parent
def __repr__(self)
django.template.loader_tags.BlockNode
Return repr(self).
def __repr__(self):
return "<Block Node: %s. Contents: %r>" % (self.name, self.nodelist)
def get_nodes_by_type(self, nodetype)
django.template.base.Node
Return a list of all nodes (within this node and its nodelist) of the given type
def get_nodes_by_type(self, nodetype):
"""
Return a list of all nodes (within this node and its nodelist)
of the given type
"""
nodes = []
if isinstance(self, nodetype):
nodes.append(self)
for attr in self.child_nodelists:
nodelist = getattr(self, attr, None)
if nodelist:
nodes.extend(nodelist.get_nodes_by_type(nodetype))
return nodes
def render(self, context)
django.template.loader_tags.BlockNode
django.template.loader_tags.BlockNode
Return the node rendered as a string.
def render(self, context):
block_context = context.render_context.get(BLOCK_CONTEXT_KEY)
with context.push():
if block_context is None:
context["block"] = self
result = self.nodelist.render(context)
else:
push = block = block_context.pop(self.name)
if block is None:
block = self
# Create new block so we can store context without thread-safety issues.
block = type(self)(block.name, block.nodelist)
block.context = context
context["block"] = block
result = block.nodelist.render(context)
if push is not None:
block_context.push(self.name, push)
return result
django.template.base.Node
Return the node rendered as a string.
def render(self, context):
"""
Return the node rendered as a string.
"""
pass
def render_annotated(self, context)
django.template.base.Node
Render the node. If debug is True and an exception occurs during rendering, the exception is annotated with contextual line information where it occurred in the template. For internal usage this method is preferred over using the render method directly.
def render_annotated(self, context):
"""
Render the node. If debug is True and an exception occurs during
rendering, the exception is annotated with contextual line information
where it occurred in the template. For internal usage this method is
preferred over using the render method directly.
"""
try:
return self.render(context)
except Exception as e:
if context.template.engine.debug:
# Store the actual node that caused the exception.
if not hasattr(e, "_culprit_node"):
e._culprit_node = self
if (
not hasattr(e, "template_debug")
and context.render_context.template.origin == e._culprit_node.origin
):
e.template_debug = (
context.render_context.template.get_exception_info(
e,
e._culprit_node.token,
)
)
raise
def super(self)
django.template.loader_tags.BlockNode
def super(self):
if not hasattr(self, "context"):
raise TemplateSyntaxError(
"'%s' object has no attribute 'context'. Did you use "
"{{ block.super }} in a base template?" % self.__class__.__name__
)
render_context = self.context.render_context
if (
BLOCK_CONTEXT_KEY in render_context
and render_context[BLOCK_CONTEXT_KEY].get_block(self.name) is not None
):
return mark_safe(self.render(self.context))
return ""