Drupal 8 Attributes code example

Example: attribute drupal 8

<div{{ attributes.addClass('my-class') }}></div>

{% set classes = [ 'red', 'green', 'blue' ] %}
<div{{ attributes.addClass(classes).removeClass('green') }}></div>

<div{{ attributes.setAttribute('id', 'myID') }}></div>
<div{{ attributes.removeAttribute('id') }}></div>
<div style="{{ attributes.style }}"></div>

{% if attributes.hasClass('myClass') %}
  {# do stuff #}
{% endif %}

{% if attributes['data-my-data'] %}
  {# do stuff #}
{% endif %}

<div class="myclass {{ attributes.class }}"{{ attributes|without('class') }}></div>

{% set my_attribute = create_attribute() %}
{%
  set my_classes = [
	'your class'
  ]
%}
<div{{ my_attribute.addClass(my_classes).setAttribute('id', 'myUniqueId') }}>
  {{ content }}
</div>

Tags:

Html Example