php 在 Twig 中,检查数组的特定键是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13607241/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:46:24  来源:igfitidea点击:

In Twig, check if a specific key of an array exists

phparraystwigconditionalshort-circuiting

提问by user852610

In PHP we can check if a key exists in an array by using the function array_key_exists().

在 PHP 中,我们可以使用函数来检查数组中是否存在键array_key_exists()

In the Twig templating language we can check if an variable or an object's property exists simply by using an ifstatement, like this:

在 Twig 模板语言中,我们可以通过使用if语句来检查变量或对象的属性是否存在,如下所示:

{% if app.user %}
do something here
{% else %}
do something else
{% endif %}

But how do we check if a key of an arrayexists using Twig? I tried {% if array.key %}, but it gives me an error:

但是我们如何使用 Twig检查数组的键是否存在?我试过了{% if array.key %},但它给了我一个错误:

Key "key" for array with keys "0, 1, 2, 3...648" does not exist

As one of the primary ways of passing data into a template is using arrays, it seems like there should be some way of doing this. Any thoughts?

由于将数据传递到模板的主要方法之一是使用数组,因此似乎应该有某种方法可以做到这一点。有什么想法吗?

回答by phpisuber01

Twig example:

树枝示例:

{% if array.key is defined %}
  // do something
{% else %}
  // do something else
{% endif %}

回答by Samir Patel

You can use the keystwig function

您可以使用keys树枝功能

{% if myVar in someOtherArray|keys %}

{% if myVar in someOtherArray|keys %}

回答by dreftymac

Quick Answer (TL;DR)

快速回答 (TL;DR)

  • DeveloperTLindel wants to test for existence of array key in Twig.
  • DeveloperTLindel wants to trap any errors associated with undefined key.
  • This can be handled using the defaultfilter.
  • DeveloperTLindel 想要测试 Twig 中是否存在数组键。
  • DeveloperTLindel 想要捕获与未定义键相关的任何错误。
  • 这可以使用default过滤器处理。

Detailed Answer

详细解答

Context

语境

  • Twig 2.x (latest version as of Wed 2017-03-08)
  • General-purpose use of the defaultfilter.
  • Twig 2.x(截至 2017-03-08 的最新版本)
  • default过滤器的通用用途。

Problem

问题

  • Scenario:
  • DeveloperTLindel wants to test for existence of array key in Twig.
  • DeveloperTLindel wants to avoid any errors or exceptions caused by potentially undefined key.
  • 设想:
  • DeveloperTLindel 想要测试 Twig 中是否存在数组键。
  • DeveloperTLindel 希望避免由可能未定义的键引起的任何错误或异常。

Solution

解决方案

  • DeveloperTLindel can use the defaultfilter.
  • The defaultfilter catches any exceptions owing to undefined variable, and allows short-circuit substition of an alternate value.
  • The defaultfilter is chainable.
  • DeveloperTLindel 可以使用default过滤器。
  • default过滤器捕捉由于未定义的变量的任何异常,并允许替代值的短路substition。
  • default过滤器可链接。

Example01

示例01

{#- ****************************************
  testing for a single key in associative array
  -#} 
  {%- set mystring = myarray['key-no-existo'] |default('__BLANK__')  -%}

{#- ****************************************
  testing for a multiple keys in associative array
  -#} 
  {%- set mystring = myarray['alpha']
        |default(myarray['bravo'])
        |default(myarray['charlie'])
        |default('__BLANK__')
        -%}

See also

也可以看看