Add a class to a Twig link item in Drupal 8
{% set link_attributes = create_attribute().addClass('navbar-item') %}
{% if items %}
{% for item in items %}
{{ link(item.title, item.url, link_attributes) }}
{% endfor %}
{% endif %}
Simplified/abstracted from https://gitlab.com/agaric/patternlibrary/blob/master/agaric/templates/navigation/menu.html.twig
Thanks to a comment on a somewhat related article:
Submitted by gambry (not verified) on Thu, 06/14/2018 - 15:17
Also very useful and apparently not easily findable “How to add attributes to a link”: {% set attributes = attributes.addClass(‘button’) %} {{ link(link_text, link_url, attributes) }}