0

我知道这不完全是 Django 模板哲学,但我希望能够根据我在 http 响应上下文中指定的插件列表包含不同的模板。例如,如果我配置了以下插件:

 context['plugins'] = ['weather']

我尝试将每个模板包含在基本模板文件中:

{% for custom_plugin in custom_plugins %}
    {% include "map/plugins/custom/{{ plugin }}/includes.html" %}
{% endfor %}

我也试过:

{% for plugin in plugins %}
    @register.inclusion_tag("map/plugins/custom/{{ plugin }}/includes.html", takes_context=True)
{% endfor %}

目前,每个插件将仅在其 include.html 文件中包含脚本引用和 css 类:

<script type="text/javascript" src="{{ MEDIA_URL }}map/js/plugins/custom/weather/weatherStation.js?ver={{ version }}"></script>

有什么建议么?

4

1 回答 1

1

你的第一种方法似乎是最好的,这个答案可能会提供一些关于你如何去做的指示:How to concatenate strings in django templates?

您基本上想构建一个模板字符串以包含在带有 with 标记的变量中,然后包含它。

于 2012-06-23T21:20:01.317 回答