Pass a template tag to a child template

261 Views Asked by At

I need to pass the result of a template tag to a child template.

Parent template:

{% template_tag param1 param2 as templink %}
{% include "child_template.html" with templink1=templink %}

child_template.html:

<a href="">Download</a>

The result of the template tag is a url which is an input to the href in the child template. The template tag is a simple_tag. Using 'as' for variable assignment breaks the app.

What are the possible alternatives to evaluate the template tag and pass the url to the child template?

1

There are 1 best solutions below

1
Sachin On BEST ANSWER

Accept the context in the templatetag template_tag and store the result in the context.

Example:

# templatetags.py

@register.simple_tag(takes_context=True)
def template_tag(context, param1, param2):
    result = foo_bar_processor(param1, param2)
    context['foo_bar'] = result
    return result

<!-- parent_template.html -->
{% template_tag param1 param2 %}

{% include "child-template.html" with templink1=foo_bar %}