目的是检查变量是否为整数,如果是,则插入hello。
尝试
{% if int(variable) %} hello {% endif %}
结果
'int' is undefined"
答案 0 :(得分:8)
对于使用Salt的人来说,当放入盐栈状态时,这对我不起作用。
{% if variable|number %} hello {% endif %}
但确实有效:
{% if variable is number %} hello {% endif %}
答案 1 :(得分:5)
int
内置过滤器(尝试将值转换为int):您需要使用过滤器格式,如下所示:
{% if variable|int != 0 %} hello {% endif %}
默认情况下,如果转换为int失败,则返回0,但您可以通过指定其他默认值作为第一个参数来更改此值。对于{0}可能是variable
的有效值的情况,我将其更改为-1。
{% if variable|int(-1) != -1 %} hello {% endif %}
请参阅:Jinja2 Docs - int builtin filter了解更多信息
number
内置测试(如果变量已经是数字则返回true):更好的解决方案,而不是使用int
过滤器(将类似字符串的整数转换为int)是使用内置测试number
,如下所示:
{% if variable is number %} hello {% endif %}
答案 2 :(得分:0)
这些解决方案都不适合我,但这确实可行:
options: {
legend:{
display:false
},
layout: {
padding: {
left: 0,
right: 40,
top: 0,
bottom: 0
}
},
scales: {
xAxes: [{
gridLines: {
display:false,
},
categoryPercentage: 0.5,
barPercentage: 0.4,
display:true
}],
yAxes: [{
display:true,
gridLines: {
lineWidth: 30,
drawBorder: false,
color:"#F0F2F5",
},
ticks:{
padding:10
}
}]
}
}[enter image description here][1]