theming – Is there a way to output the machine name of a taxonomy term?

I found this to work in the taxonomy term individual twig template: taxonomy-term.html.twig :

{% set term_machine_name = term.name.value|raw|lower|replace({' ':'-'})|replace({',':'-'}) %}

Then use term_machine_name as required.

If you are outputting the Taxonomy term: Name field in a View, I found this to work, in the rewrite results setting in the Views Display Config, when editing a View in the site’s admin pages:

<a href="#{{ name |lower|replace({' ':'-'})|replace({',':'-'}) }}">{{ name__value }}</a>