Using attributes in templates

In Drupal 8 twig template use {{ attribute }} variable heavily. You should know about {{ attribut }} variable to add/change/edit that variable.

It should look something like this:

  <?div{{ attributes }}?><?/div?> 

Attribute's methods

Here are some of the helpful methods you can use with the Attribute object:


Adds classes or merges them on to array of existing CSS classes.
Single class:
 <div{{ attributes.addClass('my-class') }}< </div<
Multiple classes:
set classes = [
<div{{ attributes.addClass(classes) }}></div>
outputs <div class="red green"></div>.


Removes a class from an Attribute object. Used similarly to addClass.

attributes.setAttribute($attribute, $value)

Sets values for an attribute key.

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

Sample blackbox block

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas non porta odio, id mattis erat.

Nam sed tincidunt eros. Donec a venenatis odio, vitae commodo sem. Vivamus ullamcorper pellentesque erat, in adipiscing tortor scelerisque sit amet. Nulla pulvinar urna magna. Vivamus a ipsum lorem. Etiam adipiscing pulvinar lorem.