admin管理员组文章数量:1417548
I am trying to pass a parameter to a context_processor function.
@app.context_processor
def my_utility_processor():
def foo(var):
return var
return dict(foo=foo)
The jinja/html template part looks like that:
<button type="button" class="btn btn-info" onclick="var name={{ i.DeviceName }};this.disabled=true;alert('{{ foo({{ variable }}) }} ')">
Click</button>
Is it somehow possible to pass a jinja2 parameter to a jinja function call?
I am trying to pass a parameter to a context_processor function.
@app.context_processor
def my_utility_processor():
def foo(var):
return var
return dict(foo=foo)
The jinja/html template part looks like that:
<button type="button" class="btn btn-info" onclick="var name={{ i.DeviceName }};this.disabled=true;alert('{{ foo({{ variable }}) }} ')">
Click</button>
Is it somehow possible to pass a jinja2 parameter to a jinja function call?
Share Improve this question edited Dec 17, 2015 at 14:37 Synal asked Dec 17, 2015 at 13:59 SynalSynal 853 silver badges11 bronze badges2 Answers
Reset to default 2Once you enter a {{ ... }}
block you are governed by Python's syntax.
alert('{{ foo(variable) }} ')"
Depending on the output of foo
, though, this could cause a JavaScript error. You can go one step further and make Jinja format the output for you
alert({{ foo(variable)|tojson|safe }})
This will take care of things like quoting strings for you.
From what I read here: https://sopython./canon/103/use-jinja-variable-in-function-call/
"Don’t nest curly braces within a Jinja expression, you’re already in the expression"
So for your case, you should simply write:
alert('{{ foo(variable) }}')
(Note: I didn't try it yet)
本文标签: javascriptHow to pass parameters to a jinja2 flask function callStack Overflow
版权声明:本文标题:javascript - How to pass parameters to a jinja2 flask function call? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745270746a2650876.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论