mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-30 20:16:45 +00:00
Add slug filter (follow-up to PR #3386)
This commit is contained in:
committed by
Fabien Potencier
parent
1a0162a612
commit
0fd67941be
@@ -47,6 +47,7 @@ Filters
|
||||
reverse
|
||||
round
|
||||
slice
|
||||
slug
|
||||
sort
|
||||
spaceless
|
||||
split
|
||||
|
||||
@@ -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 <https://symfony.com/doc/current/components/string.html#slugger>`_.
|
||||
|
||||
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)
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -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": {
|
||||
|
||||
Reference in New Issue
Block a user