Twig String Extra
Twig extension bridging Symfony's String component filters into templates
Repository Health
Technical Analysis
Twig String Extra is a small Twig extension that exposes Symfony’s String component utilities as template filters: u wraps a string in a UnicodeString object for chainable Unicode-aware operations, slug generates URL-safe slugs via AsciiSlugger, and singular/plural convert word forms using locale-aware inflectors (English, French, and, when available, Spanish).
It’s one of the official twig/*-extra packages maintained alongside core Twig, meant to be installed only when a project needs these specific filters rather than bundled into Twig core. Because it depends on symfony/string, it’s most commonly pulled in by Symfony applications, though the Twig extension itself works in any project that has both Twig and Symfony’s String component installed.
What You Get
- The
ufilter, wrapping any string in a chainableUnicodeStringobject with dozens of Unicode-safe methods - The
slugfilter, generating locale-aware URL slugs through Symfony’sAsciiSlugger - The
singularandpluralfilters, converting word forms via English, French, and Spanish inflectors - A single
StringExtensionclass implementing Twig’sAbstractExtension, requiring no additional configuration beyond registering it
Common Use Cases
- Generating SEO-friendly URL slugs from article titles directly inside a Twig template
- Pluralizing or singularizing dynamic labels (e.g., “1 item” vs. “3 items”) without writing PHP helper functions
- Chaining Unicode string transformations (trim, case conversion, truncation) inline in a template via the
ufilter - Adding lightweight text-formatting filters to a Symfony app that already depends on
symfony/string
Under The Hood
Architecture - The entire package is a single StringExtension class extending Twig’s AbstractExtension, registering four TwigFilter instances (u, slug, plural, singular) whose callbacks delegate directly to Symfony String primitives (UnicodeString, AsciiSlugger, and per-locale InflectorInterface implementations lazily instantiated and cached per instance). Tech Stack - Pure PHP 8.1+, depending on symfony/string, symfony/translation-contracts, and twig/twig (supporting both Twig 3.x and 4.x) with no other runtime dependencies. Code Quality - Test coverage consists of a single IntegrationTest.php exercising the filters end-to-end; the small surface area and thin delegation to well-tested Symfony components keeps the risk of untested logic low despite minimal dedicated tests. API Design - The four filters map one-to-one onto existing Symfony String methods with minimal parameter surface (separator/locale for slug, locale/$all flag for plural/singular), making the extension trivial to learn for anyone already familiar with Symfony’s String component.
Used by 3 apps in this directory
Kimai
Invoicing Finance · Project Management
Professional open-source time tracking with invoicing, multi-user support, SAML/LDAP auth, and a full REST API—self-host it or use the cloud.
Mautic
Automation · Marketing · Ecommerce
The world's largest open source marketing automation platform — own your data, run multi-channel campaigns, and escape vendor lock-in forever.
wallabag
Bookmarks Archiving
Self-hosted read-it-later app that saves clean, ad-free articles from any webpage for distraction-free reading across all your devices.