Apologies if this has been asked before, but I'm on my first week working with drupal and twig.

I have the following code:

  set container_classes = [
    'paragraph--type--' ~ paragraph.bundle|clean_class,
    view_mode ? 'paragraph--view-mode--' ~ view_mode|clean_class,
    not paragraph.isPublished() ? 'paragraph--unpublished',
{% set image_classes = [
{% block paragraph %}  
  {% block content %}
    <div{{ attributes.addClass(container_classes) }}>
      <div class='row'>
        <div{{ attributes.addClass(image_classes) }} data-type='image'>
          {{ content.field_two_column_image }}          
        <div class='col-12 col-lg-auto' data-type='copy'>
          {{ content.field_two_column_copy }}
  {% endblock %}
{% endblock paragraph %}

My issue is the nested attributes.addClass. When I look at the HTML, I'm also seeing the container_classes classes, which is not what I'm looking for.

So how can I separate the two?

  • {{ attributes }} is the same array as it is used through your file. When you .addClass() it is added to that array. If you have nested elements and apply that array you will need to .removeClass() if you don’t want particular classes to cascade to those child elements Dec 16, 2020 at 21:56
  • So something like this <div{{ attributes.addClass(container_classes).removeClass(container) }} data-type='image'> and ensure all my classes are in the one array?
    – Mark Handy
    Dec 16, 2020 at 22:05
  • 2
    That syntax is correct. This DA might be helpful: drupal.stackexchange.com/questions/250300/…. @CamelCode provides a really good option, though for certain nested elements it might make more sense to set classes manually with html than with the attributes array. e.g. class="" on the element. Really depends on if these classes will be reused or not, and if a new attribute array makes sense for your use case. Dec 16, 2020 at 22:16
  • I would normally set them manually, but the component has options (image left or right) that I'm using to set a class. I don't build my prototypes with everything a parent class on the outer div, so i'll need to make some adjustments.
    – Mark Handy
    Dec 17, 2020 at 12:30

1 Answer 1


attributes is a variable containing attributes information (class, id, others..) for the main container of the template.
It should usually be used only once.

You can create a new attribute variable like this :

{% set my_attribute = create_attribute() %}|

And you can use it as usual :

<div{{ my_attribute.addClass(image_classes).setAttribute('data-type', 'image') }}>
  {{ content.field_two_column_image }}          

More information about using the attributes variable in Twig templates :

  • Ok thanks. I'll try that and see if I can get what I need to work. Classes would make it easier, but a data attribute might work too
    – Mark Handy
    Dec 16, 2020 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.