我需要将模板标签的结果传递给子模板。
父模板:
{% template_tag param1 param2 as templink %}
{% include "child_template.html" with templink1=templink %}child_template.html:
<a href="">Download</a>
模板标签的结果是一个url,它是子模板中href的输入。模板标记是一个simple_tag。使用“as”作为变量赋值会破坏应用程序。
评估模板标记并将url传递给子模板的可能选择是什么?
发布于 2018-09-10 14:52:34
在templatetag template_tag中接受上下文,并将结果存储在上下文中。
示例
# 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 %}https://stackoverflow.com/questions/52260078
复制相似问题