Ramita: en array o similar posible dentro de la declaración if?


Estoy usando Twig como motor de plantillas y realmente me encanta. Sin embargo, ahora he corrido en una situación que definitivamente debeser realizable de una manera más simple de lo que he encontrado.

Lo que tengo ahora mismo es esto:

{% for myVar in someArray %}    
    {% set found = 0 %}
    {% for id, data in someOtherArray %}
        {% if id == myVar %}
            {{ myVar }} exists within someOtherArray.
            {% set found = 1 %} 
        {% endif %}
    {% endfor %}

    {% if found == 0 %}
        {{ myVar }} doesn't exist within someOtherArray.
    {% endif %}
{% endfor %}

Lo que estoy buscando es algo más como esto:

{% for myVar in someArray %}    
    {% if myVar is in_array(array_keys(someOtherArray)) %}
       {{ myVar }} exists within someOtherArray.
    {% else %}
       {{ myVar }} doesn't exist within someOtherArray.
    {% endif %}
{% endfor %}

¿Hay alguna manera de lograr esto que aún no he visto?

Si necesito crear mi propia extensión, ¿cómo puedo acceder a myVar dentro de la prueba ¿función?

Gracias por su ayuda!

 165
Author: sprain, 2011-09-18

4 answers

Solo tienes que cambiar la segunda línea de tu segundo bloque de código de

{% if myVar is in_array(array_keys(someOtherArray)) %}

A

{% if myVar in someOtherArray|keys %}

En está el operador de contención y las claves un filtro que devuelve las claves de los arrays.

 382
Author: Raffael,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-30 04:42:10

Solo para aclarar algunas cosas aquí. La respuesta que fue aceptada no hace lo mismo que PHP in_array.

Para hacer lo mismo que PHP in_array use la siguiente expresión:

{% if myVar in myArray %}

Si quieres negar esto deberías usar esto:

{% if myVar not in myArray %}
 62
Author: Wim Mostmans,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-05 09:45:03

Otro ejemplo siguiendo a @ jake stayman:

{% for key, item in row.divs %}
    {% if (key not in [1,2,9]) %} // eliminate element 1,2,9
        <li>{{ item }}</li>
    {% endif %}
{% endfor %}
 8
Author: Dung,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-18 14:52:08

Debería ayudarte.

{% for user in users if user.active and user.id not 1 %}
   {{ user.name }}
{% endfor %}

Más información: http://twig.sensiolabs.org/doc/tags/for.html

 1
Author: FDisk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-12 13:30:15