How to handle nested template variables?

杀马特。学长 韩版系。学妹 提交于 2019-12-13 06:55:33

问题


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.


回答1:


Try the add filter (docs here). It can chain together strings and variable values (assuming they're also strings) inside a template tag.

Example:

{% static 'docs/ver600/'|add:doc_name|add:'/_build/html/index.html' %}


来源:https://stackoverflow.com/questions/26391133/how-to-handle-nested-template-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!