如何在 Twig 中检查 null ?

发布于 2024-09-10 00:20:12 字数 39 浏览 10 评论 0原文

我应该使用什么构造来检查 Twig 模板中的值是否为 NULL?

What construct should I use to check whether a value is NULL in a Twig template?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

忱杏 2024-09-17 00:20:13

根据您的具体需要:

{% if var is null %}
    {# do something #}
{% endif %}
{% if var is not defined %}
    {# do something #}
{% endif %}

此外 是Sameas 测试对两个值进行类型严格比较,可能对检查 null 以外的值(如 false)感兴趣:

{% if var is sameas(false) %}
    {# do something %}
{% endif %}

Depending on what exactly you need:

  • is null checks whether the value is null:
{% if var is null %}
    {# do something #}
{% endif %}
  • is defined checks whether the variable is defined:
{% if var is not defined %}
    {# do something #}
{% endif %}

Additionally the is sameas test, which does a type strict comparison of two values, might be of interest for checking values other than null (like false):

{% if var is sameas(false) %}
    {# do something %}
{% endif %}
清晨说晚安 2024-09-17 00:20:13

如何在twig中设置默认值: http://twig.sensiolabs.org/doc/filters /default.html

{{ my_var | default("my_var doesn't exist") }}

或者如果你不希望它在为空时显示:

{{ my_var | default("") }}

How to set default values in twig: 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:

{{ my_var | default("") }}
好听的两个字的网名 2024-09-17 00:20:13

在没有任何假设的情况下,答案是:

{% if var is null %}

但只有当 var 恰好是 NULL,而不是任何其他计算结果为 false 的值时,这才是正确的(例如零、空字符串和空数组)。另外,如果没有定义var,也会导致错误。更安全的方法是:

{% if var is not defined or var is null %}

可以缩短为:

{% if var|default is null %}

如果您没有向 default 过滤器提供参数,则它假定为 NULL (某种默认值)。因此,检查变量是否为空(null、false、空字符串/数组等)的最短、最安全的方法(我知道):

{% if var|default is empty %}

Without any assumptions the answer is:

{% if var is null %}

But this will be true only if var is 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 var is not defined. A safer way would be:

{% 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 default filter, 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):

{% if var|default is empty %}
冰葑 2024-09-17 00:20:13

我认为你不能。这是因为,如果变量在 twig 模板中未定义(未设置),则它看起来像 NULLnone (用 twig 术语来说)。我很确定这是为了抑制模板中发生错误的访问错误。

由于 Twig 中缺乏“身份”(===),这是你能做的最好的事情,

{% if var == null %}
    stuff in here
{% endif %}

这意味着:

if ((isset($context['somethingnull']) ? $context['somethingnull'] : null) == null)
{
  echo "stuff in here";
}

如果你擅长你的 类型杂耍,意味着诸如 0''< /code>、FALSENULL 和未定义的 var 也将使该语句为 true。

我的建议是要求将身份实施到 Twig 中。

I don't think you can. This is because if a variable is undefined (not set) in the twig template, it looks like NULL or none (in twig terms). I'm pretty sure this is to suppress bad access errors from occurring in the template.

Due to the lack of a "identity" in Twig (===) this is the best you can do

{% 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.

My suggest is to ask for the identity to be implemented into Twig.

趁微风不噪 2024-09-17 00:20:13

您还可以使用一行来完成此操作:

{{ yourVariable is not defined ? "Not Assigned" : "Assigned" }}

You can also use one line to do that:

{{ yourVariable is not defined ? "Not Assigned" : "Assigned" }}
橘虞初梦 2024-09-17 00:20:13
     //test if varibale exist
     {% if var is defined %}
         //todo
     {% endif %}

     //test if variable is not null
     {% if var is not null %}
         //todo
     {% endif %}
     //test if varibale exist
     {% if var is defined %}
         //todo
     {% endif %}

     //test if variable is not null
     {% if var is not null %}
         //todo
     {% endif %}
陪你搞怪i 2024-09-17 00:20:13

您可以使用以下代码来检查是否

{% if var is defined %}

var is variable is SET

{% endif %}

you can use the following code to check whether

{% if var is defined %}

var is variable is SET

{% endif %}
屋顶上的小猫咪 2024-09-17 00:20:13

此外,如果您的变量是ARRAY,也有几个选项:

{% if arrayVariable[0] is defined %} 
    #if variable is not null#
{% endif %}

或者

{% if arrayVariable|length > 0 %} 
    #if variable is not null# 
{% endif %}

只有当您的数组已定义且为NULL时,这才有效

Also if your variable is an ARRAY, there are few options too:

{% 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 defined AND is NULL

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文