Repeatation of class while adding class in twig template I want to add CSS class to &lt;li&gt;&lt;/li&gt; as shown in the following code. There are three &lt;li&gt;&lt;/li&gt; codes where I have added calss using {{attributes.addClass('')}}

from book-navigation.html.twig

{% if tree or has_links %}
  &lt;nav role="navigation" aria-labelledby="book-label-{{ book_id }}"&gt;
    {{ tree }}
    {% if has_links %}
      &lt;h2&gt;{{ 'Book traversal links for'|t }} {{ book_title }}&lt;/h2&gt;
      &lt;ul&gt;
      {% if prev_url %}
        &lt;li{{attributes.addClass('page-previous')}}&gt;
          &lt;a href="{{ prev_url }}" rel="prev" title="{{ 'Go to previous page'|t }}"&gt;&lt;b&gt;{{ '‹'|t }}&lt;/b&gt; {{ prev_title }}&lt;/a&gt;
        &lt;/li&gt;
      {% endif %}
      {% if parent_url %}
        &lt;li{{attributes.addClass('page-up')}}&gt;
          &lt;a href="{{ parent_url }}" title="{{ 'Go to parent page'|t }}"&gt;{{ 'Up'|t }}&lt;/a&gt;
        &lt;/li&gt;
      {% endif %}
      {% if next_url %}
        &lt;li{{attributes.addClass('page-next')}}&gt;
          &lt;a href="{{ next_url }}" rel="next" title="{{ 'Go to next page'|t }}"&gt;{{ next_title }} &lt;b&gt;{{ '›'|t }}&lt;/b&gt;&lt;/a&gt;
        &lt;/li&gt;
      {% endif %}
    &lt;/ul&gt;
    {% endif %}
  &lt;/nav&gt;
{% endif %}


The output I am getting is with above code is,


  


But, I want as following,

&lt;ul&gt;
  &lt;li class="page-previuos"&gt;&lt;/li&gt;
  &lt;li class="page-up"&gt;&lt;/li&gt;
  &lt;li class="page-next"&gt;&lt;/li&gt;
&lt;/ul&gt;


Am I missing something, or there is other right-way to do that? Very Thanks.
If you don't want to add a class, but to replace the existing classes, you can use setAttribute().

&lt;li{{attributes.setattribute('class', 'page-previous')}}&gt;