Gui*_*rez 31 string isnullorempty twig
如何在Twig中检查字符串变量是空或空,还是填充空格字符?(可能最短,可能等同于CSharp的String.IsNullOrWhiteSpace()方法)
Sir*_*ton 46
{% if your_variable is null or your_variable is empty %}
Run Code Online (Sandbox Code Playgroud)
应该检查变量是null还是空.
如果你想看看它是不是 null或空只使用not运营商.
{% if foo is not null and foo is not empty %}
Run Code Online (Sandbox Code Playgroud)
查看文档:
Ala*_*blo 32
已有很好的答案,但我也给了我2美分:
{% if foo|length %}
Run Code Online (Sandbox Code Playgroud)
我受到了@GuillermoGutiérrez的过滤技巧的启发.
但我认为|length更安全,因为"0"|trim表达式将评估为false.
参考文献:
Mik*_*ikO 12
{% if foo|trim is empty %}
{% if foo|trim is not empty %}
Run Code Online (Sandbox Code Playgroud)
如果 foo变量为: empty,则求值为true: