Django template - global template namespace as parameter value for inclusion tag
I have a Django template for rendering tables which I can successfully include in my other templates. I takes a prefix and a namespace parameter, where the namespace is a key within the rendering context:
{% include "table.html" with prefix=name|add:"_" namespace=rubric1 %}
and in the template I use e.g.:
{% for object in namespace.object_list %}
...
{% endfor %}
My rendering context looks like this:
context = {
'some_var': ...,
'rubric1': {
'object_list': ...,
'direction': ...,
'orderby': ...,
'total': ...,
'page': ...,
...
},
'rubric2': {
'object_list': ...,
...
},
}
Now I want to use this template in a ListView
, where those keys live at the root of the context namespace:
context = {
'some_var': ...,
'object_list': ...,
'direction': ...,
'orderby': ...,
'total': ...,
'page': ...,
...
}
How do I tell the inclusion tag to use the root of the rendering context as namespace?
Is there a special variable for this? E.g.:
{% include "table.html" with prefix=name|add:"_" namespace=global_template_namespace %}
Or can i somehow use a with
context? E.g.:
{% with global_template_namespace as ns %}
{% include "table.html" with prefix=name|add:"_" namespace=ns%}
{% endwith%}
I have also thought about simple_tag
that directly returns the context, but couldn't manage to use the result as argument either:
@register.simple_tag(takes_context=True)
def root_namespace(context):
return context