如何处理嵌套模板变量? [英] How to handle nested template variables?
问题描述
我目前拥有一个嵌入式< iframe>
,它有一个硬连线的静态网址:
I currently have an embedded <iframe>
that has a hard wired static url:
<iframe src="{% static 'docs/ver600/technical-reference-guide/_build/html/index.html' %}" frameborder="0" class="full-screen"></iframe>
我想用静态字符串中的变量替换部分。
I would like replace parts of the static string with variables.
例如,技术参考指南只是一个文档名称变量。显然,嵌入模板变量不起作用:
For example, the "technical-reference-guide" is just a document name variable. Obviously, just embedding a template variable doesn't work:
<iframe src="{% static 'docs/ver600/{{ doc_name }}/_build/html/index.html' %}" frameborder="0" class="full-screen"></iframe>
在模板中处理此类动态互动的最佳方式是什么?
What would the best way for handling this type of dynamic interaction within a template?
此外,文档名称是从页面块中派生的:
Also, the document name is derived from the page slug:
{% page_attribute 'slug' %}
如果这改变了所有的回复。
if that alters responses at all.
推荐答案
尝试添加
filter( docs here )。它可以将模板标签中的字符串和变量值(假设它们也是字符串)链接在一起。
Try the add
filter (docs here). It can chain together strings and variable values (assuming they're also strings) inside a template tag.
示例:
{% static 'docs/ver600/'|add:doc_name|add:'/_build/html/index.html' %}
这篇关于如何处理嵌套模板变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!