Using a top level variable in datatable template
kkostenko opened this issue · 1 comments
Hello!
I am wondering if somebody knows an answer to the following question. I have a template 'top_level.html' which calls a table tag to render a datatable. I have a variable in that template and I want to use it in a datatable template 'share_table.html' similar to https://github.com/shymonk/django-datatable/blob/master/table/templates/table/table.html . How would I do it? If I simply use {{ some_variable }} in share_table.html it does not work.
=== top_level.html
{{ some_variable }}
{% render_share_table table %}
== table_tags.py
class ShareTableNode(TableNode):
template_name = "share_table.html" <-- need to use {{ some_variable }} here
@register.tag
def render_share_table(parser, token):
try:
tag, table = token.split_contents()
except ValueError:
msg = '%r tag requires a single arguments' % token.split_contents()[0]
raise template.TemplateSyntaxError(msg)
return ShareTableNode(table)
ok I figured it out. Needed some customization for tags, 'p' is a variable passed from top level template to the rendered table:
class SpecialTableNode(TableNode):
template_name = "s_table.html"
def __init__(self, table, platforms):
self.table = template.Variable(table)
self.p = template.Variable(p)
def render(self, context):
table = self.table.resolve(context)
table_context = Context({'table': table})
p = self.p.resolve(context)
t = template.loader.get_template(self.template_name)
return t.render(context)
=== custom tag =====
@register.tag
def render_s_table(parser, token):
try:
tag, table, platforms = token.split_contents()
except ValueError:
msg = '%r tag requires a single arguments' % token.split_contents()[0]
raise template.TemplateSyntaxError(msg)
return SpecialTableNode(table, p)
==== top level template =====
{% render_s_table table p %}