Skip to main content
add code example from change record
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217

Your question is exactly about this issue:

Allow instantiating Attribute objects within Twig

Problem/Motivation

Sometimes you have an array of attributes and it would be nice if you could addClass, removeClass, setAttribute, etc. rather than trying to do array manipulation.

Proposed resolution

Add a function to our Twig extension to allow creating an Attribute object from an array of attributes.

Issue: https://www.drupal.org/node/2616756

And the change record for 8.38.3.x:

{% set my_attribute = create_attribute() %}
{%
  set my_classes = [
    'kittens',
    'llamas',
    'puppies',
  ]
%}
<div{{ my_attribute.addClass(my_classes).addAttribute('id', 'myUniqueId') }}>
  {{ content }}
</div>
<div{{ create_attribute({'class': ['region', 'region--header']}) }}>
  {{ content }}
</div>

https://www.drupal.org/node/2818293

Your question is exactly about this issue:

Allow instantiating Attribute objects within Twig

Problem/Motivation

Sometimes you have an array of attributes and it would be nice if you could addClass, removeClass, setAttribute, etc. rather than trying to do array manipulation.

Proposed resolution

Add a function to our Twig extension to allow creating an Attribute object from an array of attributes.

Issue: https://www.drupal.org/node/2616756

And the change record for 8.3:

https://www.drupal.org/node/2818293

Your question is exactly about this issue:

Allow instantiating Attribute objects within Twig

Problem/Motivation

Sometimes you have an array of attributes and it would be nice if you could addClass, removeClass, setAttribute, etc. rather than trying to do array manipulation.

Proposed resolution

Add a function to our Twig extension to allow creating an Attribute object from an array of attributes.

Issue: https://www.drupal.org/node/2616756

And the change record for 8.3.x:

{% set my_attribute = create_attribute() %}
{%
  set my_classes = [
    'kittens',
    'llamas',
    'puppies',
  ]
%}
<div{{ my_attribute.addClass(my_classes).addAttribute('id', 'myUniqueId') }}>
  {{ content }}
</div>
<div{{ create_attribute({'class': ['region', 'region--header']}) }}>
  {{ content }}
</div>

https://www.drupal.org/node/2818293

Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217

Your question is exactly about this issue:

Allow instantiating Attribute objects within Twig

Problem/Motivation

Sometimes you have an array of attributes and it would be nice if you could addClass, removeClass, setAttribute, etc. rather than trying to do array manipulation.

Proposed resolution

Add a function to our Twig extension to allow creating an Attribute object from an array of attributes.

Issue: https://www.drupal.org/node/2616756

And the change record for 8.3:

https://www.drupal.org/node/2818293