Jinja模板中的嵌套打印语句 [英] Nested Print statements in jinja template

查看:54
本文介绍了Jinja模板中的嵌套打印语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用flask在jinja2模板中创建链接,但遇到了问题.我想访问在代码块中传递给模板的变量,但是我不确定如何执行或什至可能. 我正在尝试做的事情:

I am creating a link in a jinja2 template using flask and i am running into a problem. I want to access a variable passed to a the template within a code block, but i am unsure how to do it or if it is even possible. What i am trying to do:

a href="{{ url_for('myRoute', varToBePassed = {{templateVar}} ) }}" >

所以基本上我想在模板中使用url_for函数将查询参数传递给变量,但是它不喜欢嵌套的双花括号.我也可以像这样:

So basically i want to pass a query parameter to a variable using the url_for function in a template, but it doesnt like the nested double curly braces. I could alternatively just do it like:

a href="/myRoute/{{templateVar}}" >

这行得通,但是我觉得如果我不得不在文件结构中移动内容,将来可能会遇到问题.

which works, but i feel like i could run into problems in the future if i have to move things in the file structure.

有没有办法将花括号嵌套在Jinja模板中?还是将变量传递到Jinja中的代码块的另一种方法?我查看了宏,但似乎也遇到了同样的问题,其中也嵌套了花括号.

Is there a way to nest curly braces in jinja templates? Or another way to pass variables to code blocks in jinja? I looked into macros, but it seemed like i would run into the same problems with nested curly braces in it, as well.

推荐答案

只需直接使用模板变量:

Just use the template variable directly:

a href="{{ url_for('myRoute', varToBePassed = templateVar ) }}" >

这篇关于Jinja模板中的嵌套打印语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆