我正在使用 Jinja2==2.7.1
In [2]: t = jinja2.Template("{{ x/2 | round }}") #does not work?
In [3]: t
Out[3]: <Template memory:161e910>
In [4]: t.render(x=7)
Out[4]: u'3.5'
In [5]: t2 = jinja2.Template("{{ x | float }}") # float works
In [6]: t2.render(x=3)
Out[6]: u'3.0'
In [12]: t3 = Template("{{ x / 2 | int }}") # does not work
In [13]: t3.render(x=7)
Out[13]: u'3.5'
我的猜测是运算符优先级。 x/2 | round
被解析为x / (2 | round)
是的,你是对的,
谢谢
In [6]: t = jinja2.Template("{{ (x/2) | int }}")
In [7]: t.render(x=5)
Out[7]: u'2'
刚刚遇到了确切的问题!
谢谢@hvnsweeting和@Naddiseo
同样的问题,同样的解决方案。 就我而言,它是在家庭助理中我最终得到的:
{{ ((states.input_number.guestroom_setpoint_shift.state | float) * 2) | int }}
谢谢!
最有用的评论
我的猜测是运算符优先级。
x/2 | round
被解析为x / (2 | round)