Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame^] | 1 | ``slug`` |
| 2 | ======== |
| 3 | |
| 4 | The ``slug`` filter transforms a given string into another string that |
| 5 | only includes safe ASCII characters. |
| 6 | |
| 7 | Here is an example: |
| 8 | |
| 9 | .. code-block:: twig |
| 10 | |
| 11 | {{ 'Wôrķšƥáçè ~~sèťtïñğš~~'|slug }} |
| 12 | Workspace-settings |
| 13 | |
| 14 | The default separator between words is a dash (``-``), but you can |
| 15 | define a selector of your choice by passing it as an argument: |
| 16 | |
| 17 | .. code-block:: twig |
| 18 | |
| 19 | {{ 'Wôrķšƥáçè ~~sèťtïñğš~~'|slug('/') }} |
| 20 | Workspace/settings |
| 21 | |
| 22 | The slugger automatically detects the language of the original |
| 23 | string, but you can also specify it explicitly using the second |
| 24 | argument: |
| 25 | |
| 26 | .. code-block:: twig |
| 27 | |
| 28 | {{ '...'|slug('-', 'ko') }} |
| 29 | |
| 30 | The ``slug`` filter uses the method by the same name in Symfony's |
| 31 | `AsciiSlugger <https://symfony.com/doc/current/components/string.html#slugger>`_. |
| 32 | |
| 33 | .. note:: |
| 34 | |
| 35 | The ``slug`` filter is part of the ``StringExtension`` which is not |
| 36 | installed by default. Install it first: |
| 37 | |
| 38 | .. code-block:: bash |
| 39 | |
| 40 | $ composer require twig/string-extra |
| 41 | |
| 42 | Then, on Symfony projects, install the ``twig/extra-bundle``: |
| 43 | |
| 44 | .. code-block:: bash |
| 45 | |
| 46 | $ composer require twig/extra-bundle |
| 47 | |
| 48 | Otherwise, add the extension explicitly on the Twig environment:: |
| 49 | |
| 50 | use Twig\Extra\String\StringExtension; |
| 51 | |
| 52 | $twig = new \Twig\Environment(...); |
| 53 | $twig->addExtension(new StringExtension()); |
| 54 | |
| 55 | Arguments |
| 56 | --------- |
| 57 | |
| 58 | * ``separator``: The separator that is used to join words (defaults to ``-``) |
| 59 | * ``locale``: The locale of the original string (if none is specified, it will be automatically detected) |