class InclusionNode
from yak.tags import InclusionNode
Django InclusionNode subclass with support for: - context assignment - dynamic template file assignation
Ancestors (MRO)
- builtins.object
- django.template.base.Node
- django.template.library.TagHelperNode
- django.template.library.InclusionNode
- yak.tags.AssignmentNodeMixin
- yak.tags.InclusionNode
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 |
template_base |
None |
yak.tags.InclusionNode |
token |
None |
django.template.base.Node |
unresolved |
None |
yak.tags.InclusionNode |
def __init__(self, func, takes_context, args, kwargs, filename)
django.template.library.InclusionNode
django.template.library.InclusionNode
Initialize self. See help(type(self)) for accurate signature.
def __init__(self, func, takes_context, args, kwargs, filename):
super().__init__(func, takes_context, args, kwargs)
self.filename = filename
django.template.library.TagHelperNode
Initialize self. See help(type(self)) for accurate signature.
def __init__(self, func, takes_context, args, kwargs):
self.func = func
self.takes_context = takes_context
self.args = args
self.kwargs = kwargs
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 get_resolved_arguments(self, context)
django.template.library.TagHelperNode
def get_resolved_arguments(self, context):
resolved_args = [var.resolve(context) for var in self.args]
if self.takes_context:
resolved_args = [context] + resolved_args
resolved_kwargs = {k: v.resolve(context) for k, v in self.kwargs.items()}
return resolved_args, resolved_kwargs
def render(self, context)
yak.tags.InclusionNode
yak.tags.InclusionNode
resolves the template filename and adapt the cache mechanism for supporting different filenames
def render(self, context):
'''
resolves the template filename and adapt the cache mechanism for supporting
different filenames
'''
self.resolve_filename(context)
context_cache = context.render_context.get(self, dict())
context.render_context[self] = context_cache.get(self.filename)
rv = super().render(context)
context_cache[self.filename] = context.render_context.get(self)
context.render_context[self] = context_cache
return rv
yak.tags.AssignmentNodeMixin
Return the node rendered as a string.
def render(self, context):
rendered = super().render(context)
if self.template_tag and self.template_tag.target_var:
context[self.template_tag.target_var] = rendered
return ''
return rendered
django.template.library.InclusionNode
Render the specified template and context. Cache the template object in render_context to avoid reparsing and loading when used in a for loop.
def render(self, context):
"""
Render the specified template and context. Cache the template object
in render_context to avoid reparsing and loading when used in a for
loop.
"""
resolved_args, resolved_kwargs = self.get_resolved_arguments(context)
_dict = self.func(*resolved_args, **resolved_kwargs)
t = context.render_context.get(self)
if t is None:
if isinstance(self.filename, Template):
t = self.filename
elif isinstance(getattr(self.filename, "template", None), Template):
t = self.filename.template
elif not isinstance(self.filename, str) and isinstance(
self.filename, Iterable
):
t = context.template.engine.select_template(self.filename)
else:
t = context.template.engine.get_template(self.filename)
context.render_context[self] = t
new_context = context.new(_dict)
# Copy across the CSRF token, if present, because inclusion tags are
# often used for forms, and we need instructions for using CSRF
# protection to be as simple as possible.
csrf_token = context.get("csrf_token")
if csrf_token is not None:
new_context["csrf_token"] = csrf_token
return t.render(new_context)
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 resolve_filename(self, context)
yak.tags.InclusionNode
def resolve_filename(self, context):
if hasattr(self.filename, 'resolve'):
# filename is a `FilterExpression` and needs to be resolved
self.unresolved = self.filename
if not self.unresolved:
return
self.filename = copy(self.unresolved).resolve(context)
self.sanitize_filename()
if self.template_base:
self.filename = os.path.join(self.template_base, self.filename)
def sanitize_filename(self)
yak.tags.InclusionNode
def sanitize_filename(self):
if not self.filename.endswith('.html'):
self.filename = self.filename.replace('.', os.path.sep).replace('-', '_')
self.filename = f'{self.filename}.html'