diff --git a/doc/filters/index.rst b/doc/filters/index.rst index a35c3d76d..eea2383e5 100644 --- a/doc/filters/index.rst +++ b/doc/filters/index.rst @@ -47,6 +47,7 @@ Filters reverse round slice + slug sort spaceless split diff --git a/doc/filters/slug.rst b/doc/filters/slug.rst new file mode 100644 index 000000000..99cfa173e --- /dev/null +++ b/doc/filters/slug.rst @@ -0,0 +1,37 @@ +``slug`` +======== + +The ``slug`` filter transforms a given string into another string that +only includes safe ASCII characters. + +Here is an example: + +.. code-block:: twig + + {{ 'Wôrķšƥáçè ~~sèťtïñğš~~'|slug }} + Workspace-settings + +The default separator between words is a dash (``-``), but you can +define a selector of your choice by passing it as an argument: + +.. code-block:: twig + + {{ 'Wôrķšƥáçè ~~sèťtïñğš~~'|slug('/') }} + Workspace/settings + +The slugger automatically detects the language of the original +string, but you can also specify it explicitly using the second +argument: + +.. code-block:: twig + + {{ '...'|slug('-', 'ko') }} + +The ``slug`` filter uses the method by the same name in Symfony's +`AsciiSlugger `_. + +Arguments +--------- + +* ``separator``: The separator that is used to join words (defaults to ``-``) +* ``locale``: The locale of the original string (if none is specified, it will be automatically detected) diff --git a/extra/string-extra/README.md b/extra/string-extra/README.md index 91627d2ae..6687723bf 100644 --- a/extra/string-extra/README.md +++ b/extra/string-extra/README.md @@ -4,8 +4,13 @@ String Extension This package is a Twig extension that provides integration with the Symfony String component. -It provides a single [`u`][1] filter that wraps a text in a `UnicodeString` +It provides a [`u`][1] filter that wraps a text in a `UnicodeString` object to give access to [methods of the class][2]. +It also provides a [`slug`][3] filter which is simply a wrapper for the +[`AsciiSlugger`][4]'s `slug` method. + [1]: https://twig.symfony.com/u [2]: https://symfony.com/doc/current/components/string.html +[3]: https://twig.symfony.com/slug +[4]: https://symfony.com/doc/current/components/string.html#slugger diff --git a/extra/string-extra/StringExtension.php b/extra/string-extra/StringExtension.php index 71210dcd8..db88ca922 100644 --- a/extra/string-extra/StringExtension.php +++ b/extra/string-extra/StringExtension.php @@ -11,16 +11,27 @@ namespace Twig\Extra\String; +use Symfony\Component\String\AbstractUnicodeString; +use Symfony\Component\String\Slugger\SluggerInterface; +use Symfony\Component\String\Slugger\AsciiSlugger; use Symfony\Component\String\UnicodeString; use Twig\Extension\AbstractExtension; use Twig\TwigFilter; final class StringExtension extends AbstractExtension { + private $slugger; + + public function __construct(SluggerInterface $slugger = null) + { + $this->slugger = $slugger ?: new AsciiSlugger(); + } + public function getFilters() { return [ new TwigFilter('u', [$this, 'createUnicodeString']), + new TwigFilter('slug', [$this, 'createSlug']), ]; } @@ -28,4 +39,9 @@ final class StringExtension extends AbstractExtension { return new UnicodeString($text ?? ''); } + + public function createSlug(string $string, string $separator = '-', ?string $locale = null): AbstractUnicodeString + { + return $this->slugger->slug($string, $separator, $locale); + } } diff --git a/extra/string-extra/Tests/Fixtures/slug.test b/extra/string-extra/Tests/Fixtures/slug.test new file mode 100644 index 000000000..b3bc13de5 --- /dev/null +++ b/extra/string-extra/Tests/Fixtures/slug.test @@ -0,0 +1,13 @@ +--TEST-- +"slug" filter +--TEMPLATE-- +{{ 'Wôrķšƥáçè ~~sèťtïñğš~~'|slug }} + +{{ 'Wôrķšƥáçè ~~sèťtïñğš~~'|slug('/') }} + +--DATA-- +return [] +--EXPECT-- +Workspace-settings + +Workspace/settings diff --git a/extra/string-extra/composer.json b/extra/string-extra/composer.json index aacd0a21f..768cda9b3 100644 --- a/extra/string-extra/composer.json +++ b/extra/string-extra/composer.json @@ -17,6 +17,7 @@ "require": { "php": ">=7.2.5", "symfony/string": "^5.0", + "symfony/translation-contracts": "^1.1|^2", "twig/twig": "^2.4|^3.0" }, "require-dev": {