如何在模板代码中设置变量的值? [英] How to set a value of a variable inside a template code?
本文介绍了如何在模板代码中设置变量的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< html>
< div> Hello {{name}}!< / div>
< / html>
在测试它时,定义变量的值不会触及python代码就很有用调用此模板。所以我正在寻找这样的东西
{%set name =World%}
< html> ;
< div> Hello {{name}}!< / div>
< / html>
Django中是否存在类似的东西?
解决方案
您可能希望使用模板标签。
{%以World为名称%}
< html>
< div> Hello {{name}}!< / div>
< / html>
{%endwith%}
编辑:
使用 with标签现在是:(旧格式仍然支持)
{%,name =World greeting =Hello%}
< html>
< div> {{greeting}} {{name}}!< / div>
< / html>
{%endwith%}
Say I have a template
<html>
<div>Hello {{name}}!</div>
</html>
While testing it, it would be useful to define the value of the variable without touching the python code that invokes this template. So I'm looking for something like this
{% set name="World" %}
<html>
<div>Hello {{name}}!</div>
</html>
Does something like this exists in Django?
解决方案
You probably want the with template tag.
{% with "World" as name %}
<html>
<div>Hello {{name}}!</div>
</html>
{% endwith %}
EDIT:
The proper way to use the with tag is now: (older format is still supported)
{% with name="World" greeting="Hello" %}
<html>
<div>{{ greeting }} {{name}}!</div>
</html>
{% endwith %}
这篇关于如何在模板代码中设置变量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文