Some time you would like to have id in the twig template component to enable say: precise query/selector targeting with css, or setting attributes to enable certain behavior for javascript frameworks such as bootstrap:
<!-- Notice the #collapseOne below --><divclass="accordion"id="accordionExample"><divclass="accordion-item"><h2class="accordion-header"id="headingOne"><buttonclass="accordion-button"type="button"data-bs-toggle="collapse"data-bs-target="#collapseOne"aria-expanded="true"aria-controls="collapseOne"> Accordion Item #1
</button></h2><divid="collapseOne"class="accordion-collapse collapse show"aria-labelledby="headingOne"data-bs-parent="#accordionExample"><divclass="accordion-body"><strong>This is the first item's accordion body.</strong></div></div></div></div>
A new clean_unique_id Twig filter has been added to Drupal 10.1 and later. This can be used for getting a unique ID. The filter ensures that even if the template rendered multiple times, the ID remains unique for each usage.