Dynamic template "Includes" with django
I am building a Django website and my side bar can have different elements for different users. So my main sidebar template has a div for every plugin to be included and the specific HTML for every one of these plugins is included in their own template file.
example:
<div id="plugins">
<div id="plugin1">
{% include 'plugin1.html' %}
</div>
<div id="plugin2">
{% include 'plugin2.html' %}
</div&开发者_StackOverflowgt;
</div>
Now I want to build this list dynamically how could I do it? As the template is only parsed once so I could not send it a '{% include 'plugin1.html'}' string in the context
Any ideas?
You can use a variable inside the include
tag:
{% include my_user_html %}
You can generate a variable in the view as above containing your template or you can use a template tag to generate the template path for you based on another variable, i.e. a phase. Register the following tag, customize it to your needs:
@register.filter
def get_template_phase(template_string, phase):
template_string = template_string.replace('<', '{').replace('>', '}')
return template_string.format(phase=phase)
Place the above in your templatetags and register it.
Usage:
{% include 'includes/home__<phase>.html'|get_template_phase:'nomination' %}
精彩评论