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',
'paragraph--type--' ~ paragraph.bundle|clean_class,
view_mode ? 'paragraph--view-mode--' ~ view_mode|clean_class,
not paragraph.isPublished() ? 'paragraph--unpublished',
'container'
]
%}
{% set image_classes = [
'col-12'
]
%}
{% 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>
<div class='col-12 col-lg-auto' data-type='copy'>
{{ content.field_two_column_copy }}
</div>
</div?>
</div>
{% 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@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.