Python 在Jinja2中将变量传递给宏
我制作了一些小宏,用于显示文本行和标签:Python 在Jinja2中将变量传递给宏,python,google-app-engine,jinja2,Python,Google App Engine,Jinja2,我制作了一些小宏,用于显示文本行和标签: {% macro input(name, text, help_text, value="", input_type) -%} <label for="id_{{name}}">{{text}}<span class="right">{{help_text}}</span></label> <input id="id_{{name}}" name="{{name}}" value="{
{% macro input(name, text, help_text, value="", input_type) -%}
<label for="id_{{name}}">{{text}}<span class="right">{{help_text}}</span></label>
<input id="id_{{name}}" name="{{name}}" value="{{value}}" type="{{input_type}}" />
{{%- endmacro %}
当我用{{value\u username}}
作为参数调用input时,我无法让它工作,我总是得到一个错误
你知道什么解决办法吗?我怎么能把{{value\u username}}
作为参数调用。我相信
{{ input("username", "Korisničko ime:", "Pomoć", value_username, "text") }}
应该有用尽管埃米特·J·巴特勒已经给出了答案,但在宏参数的排序上有一点挑剔。您当前使用以下签名:
input(name, text, help_text, value="", input_type)
应始终将包含默认值的参数放在所有其他必需参数之后,因此将顺序更改为:
input(name, text, help_text, input_type, value="")
现在,当使用变量作为参数调用宏时,您不需要用
{{}
包围您的变量,因为您已经在{%…%}
中了。谢谢,即使使用过滤器,它也非常有效。但是,如果有其他字符串附加或预先添加到变量中(例如=>“Hello{{{value_username | capitalize}},早上好!”),是否可以将此类字符串传递给宏?我试图避免使整个字符串成为单个变量,因为有许多预处理和附加组合。
input(name, text, help_text, input_type, value="")