将变量传递给Jinja2中的宏

dep*_*oul 9 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="{{value}}" type="{{input_type}}" />
{{%- endmacro %}
Run Code Online (Sandbox Code Playgroud)

问题是当我调用jinja2宏时:

{{input("username", "Korisni?ko ime:", "Pomo?", {{value_username}}, "text")}
Run Code Online (Sandbox Code Playgroud)

当我用{{value_username}}参数调用输入时,我无法使它工作,我总是得到一个错误.

你知道任何解决方案我怎么称呼{{value_username}}为参数.

Emm*_*ler 14

我相信

{{ input("username", "Korisni?ko ime:", "Pomo?", value_username, "text") }}
Run Code Online (Sandbox Code Playgroud)

应该管用

  • 谢谢,即使使用过滤器也能很好用.但是,如果有其他字符串追加或准备好变量,(例如=>"Hello {{value_username | capitalize}},早上好!"),是否可以将这种字符串传递给宏?我试图避免使整个字符串成为单个变量,因为有许多准备和追加组合. (5认同)