Twig

The flexible, fast, and secure
template engine for PHP

a Symfony Product
Docs Filters slug
You are reading the documentation for Twig 2.x. Switch to the documentation for Twig 1.x. 3.x.
Warning Twig version 2.x end of maintenance is scheduled for December 2023.

Questions & Feedback

License

Twig documentation is licensed under the new BSD license.

slug

The slug filter transforms a given string into another string that only includes safe ASCII characters.

Here is an example:

1
2
{{ '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:

1
2
{{ '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:

1
{{ '...'|slug('-', 'ko') }}

The slug filter uses the method by the same name in Symfony's
AsciiSlugger.

Note

The slug filter is part of the StringExtension which is not installed by default. Install it first:

1
$ composer require twig/string-extra

Then, on Symfony projects, install the twig/extra-bundle:

1
$ composer require twig/extra-bundle

Otherwise, add the extension explicitly on the Twig environment:

1
2
3
4
use Twig\Extra\String\StringExtension;

$twig = new \Twig\Environment(...);
$twig->addExtension(new StringExtension());

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)
Website powered by Symfony and Twig, deployed on
The Twig logo is © 2010-2024 Symfony