Skip to content

Instantly share code, notes, and snippets.

@megclaypool
Last active February 20, 2023 18:00
Show Gist options
  • Save megclaypool/64b27841241dbab95c2e22727c03218f to your computer and use it in GitHub Desktop.
Save megclaypool/64b27841241dbab95c2e22727c03218f to your computer and use it in GitHub Desktop.
[How to unset an element in an array in twig]

Reference:
Twig : Unset element from array - Stack Overflow

Simple Example:

{% if link.attributes is defined %}
  {# we want to get rid of the attributes element #}
  {% set link = link|filter((v, k) => k != 'attributes') %}
{% endif %}

Nested Example:

{% if link.attributes.class is defined %}
    {# we want to get rid of the class element inside the attributes #}
    {% set link =
      link|merge({
        attributes: link.attributes | filter((v, k) => k != 'class')
      })
    %}
  {% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment