php 如何在 Twig 中检查空值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3264889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check for null in Twig?
提问by Fluffy
What construct should I use to check whether a value is NULL in a Twig template?
我应该使用什么构造来检查 Twig 模板中的值是否为 NULL?
回答by NikiC
Depending on what exactly you need:
取决于你到底需要什么:
is nullchecks whether the value isnull:{% if var is null %} {# do something #} {% endif %}is definedchecks whether the variable is defined:{% if var is not defined %} {# do something #} {% endif %}
is null检查该值是否为null:{% if var is null %} {# do something #} {% endif %}is defined检查变量是否已定义:{% if var is not defined %} {# do something #} {% endif %}
Additionally the is sameastest, which does a type strict comparison of two values, might be of interest for checking values other than null(like false):
此外is sameas,对两个值进行类型严格比较的测试可能对检查除null(如false)以外的值感兴趣:
{% if var is sameas(false) %}
{# do something %}
{% endif %}
回答by lax4mike
How to set default values in twig: http://twig.sensiolabs.org/doc/filters/default.html
如何在树枝中设置默认值:http: //twig.sensiolabs.org/doc/filters/default.html
{{ my_var | default("my_var doesn't exist") }}
Or if you don't want it to display when null:
或者,如果您不希望它在 null 时显示:
{{ my_var | default("") }}
回答by Jamol
Without any assumptions the answer is:
没有任何假设,答案是:
{% if var is null %}
But this will be true only if varis exactly NULL, and not any other value that evaluates to false(such as zero, empty string and empty array). Besides, it will cause an error if varis not defined. A safer way would be:
但这只有在var是 是 时才是真的NULL,而不是任何其他评估为的值false(例如零、空字符串和空数组)。此外,如果var未定义,则会导致错误。更安全的方法是:
{% if var is not defined or var is null %}
which can be shortened to:
可以缩短为:
{% if var|default is null %}
If you don't provide an argument to the defaultfilter, it assumes NULL(sort of default default). So the shortest and safest way (I know) to check whether a variable is empty (null, false, empty string/array, etc):
如果您不为default过滤器提供参数,则它假定NULL(某种默认默认值)。因此,检查变量是否为空(空、假、空字符串/数组等)的最短和最安全的方法(我知道):
{% if var|default is empty %}
回答by Kendall Hopkins
I don't think you can. This is because if a variable is undefined (not set) in the twig template, it looks like NULLor none(in twig terms). I'm pretty sure this is to suppress bad access errors from occurring in the template.
我不认为你可以。这是因为如果一个变量在树枝模板中未定义(未设置),它看起来像NULL或none(在树枝术语中)。我很确定这是为了抑制模板中发生的错误访问错误。
Due to the lack of a "identity" in Twig (===) this is the best you can do
由于 Twig ( ===) 中缺少“身份”,这是您能做的最好的事情
{% if var == null %}
stuff in here
{% endif %}
Which translates to:
翻译成:
if ((isset($context['somethingnull']) ? $context['somethingnull'] : null) == null)
{
echo "stuff in here";
}
Which if your good at your type juggling, means that things such as 0, '', FALSE, NULL, and an undefined var will also make that statement true.
其中,如果你的好你的类型杂耍,意味着的东西,如0,'',FALSE,NULL,未定义的变种也会作出这样的说法正确。
My suggest is to ask for the identity to be implemented into Twig.
我的建议是要求在 Twig 中实现身份。
回答by thepaulo
//test if varibale exist
{% if var is defined %}
//todo
{% endif %}
//test if variable is not null
{% if var is not null %}
//todo
{% endif %}
回答by xavit
You can also use one line to do that:
您也可以使用一行来做到这一点:
{{ yourVariable is not defined ? "Not Assigned" : "Assigned" }}
回答by M.M.H.Masud
you can use the following code to check whether
您可以使用以下代码来检查是否
{% if var is defined %}
var is variable is SET
{% endif %}
回答by Cagrison
Also if your variable is an ARRAY, there are few options too:
此外,如果您的变量是ARRAY,则也有几个选项:
{% if arrayVariable[0] is defined %}
#if variable is not null#
{% endif %}
OR
或者
{% if arrayVariable|length > 0 %}
#if variable is not null#
{% endif %}
This will only works if your array is definedAND is NULL
这仅适用于您的数组is definedAND 是NULL

