I have tried to apply my custom class to an image tag

I have set classes for it

{% set imgClass = [
        'img-shape-' ~ (node.field_image_animation_shape.value|clean_class)
      ] %}

and have tried to apply to image as attributes


that is renders all attributes for the node which duplicating on main node div so i need to avoid using attributes... How do i do it?

2 Answers 2


You simply create your own attributes. Read [Documentation] Using attributes in templates. It's much easier to maintain in the long run. Especially if you start to use conditions to add or remove attributes.

This provides a new blank Attribute object to use to build up attributes.

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

I have workaround this without using attributes and that how i did it:

 {% set imgClass = node.field_image_animation_shape.value|clean_class %} 

created variable from the list text field - used key value for it

and then printed in hard way in to image classes

<img class="img-fluid img-shape-{{ imgClass }}" src="..." />

and this is result :

enter image description here

hope it will help to anybody :)

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.