Django模板语言包含变量

时间:2015-03-12 04:28:17

标签: django django-templates

include documentation中,他们声明您可以包含变量中包含的模板。但是他们没有解释如何编写模板变量。

我希望做类似的事情:

{% variable thing %}
  <div> Hello {{name}} </div>
{% endvariable thing %}

{# include the variables #}
{% include thing with name='Chris' %}
{% include thing with name='Julie' %}

2 个答案:

答案 0 :(得分:1)

只需创建模板thing.html

<div>Hello {{name}}</div>

然后在主模板中包含它:

{% include 'thing.html' with name='Chris' %}
{% include 'thing.html' with name='Julie' %}

答案 1 :(得分:0)

对于您的示例,您需要在视图中向模板上下文添加名为“thing”的键。 'thing'需要一个像“foo / bar.html”这样的值,这是一个模板的路径。

return render_to_response('index.html', {
    'thing': 'foo/bar.html'}, RequestContext(request))
相关问题