问题
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