2

Is there a twig filter to encode special characters like umlauts: {{ entry.title | filter }}

Datensätze & Karten

Which then should result in:

datensaetze-karten

Basically what 'convertFilenamesToAscii' => true, does but as a filter.

I want to use this as a anchorlink later on so umlauts are not the best to have in anchor links.

2 Answers 2

3

I would use a combination of things:

  • Twig's replace filter to convert ä to ae etc.
  • Twig's convert_encoding filter to eliminate any remaining non-ascii characters
  • Craft's kebab filter to do its thing

and of course wrap the thing up in a macro

{% macro ascii_kebab(s) %}
  {{ s
    | replace({
        'ä': 'ae'
      })
    | convert_encoding('ASCII', 'UTF-8')
    | kebab
  }}
{% endmacro %}
2
  • Thanks a lot for this! BTW: in craft 3 macros cannot contain dashes.
    – outline4
    Commented Nov 10, 2018 at 14:02
  • @outline4 whoops! removed the dash. Commented Nov 11, 2018 at 11:02
1

You can use Craft's |ascii Twig Filter, which was added in Craft 3.1.25, for this:

{{ entry.title|ascii }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.