Twig String Extra

Twig extension bridging Symfony's String component filters into templates

Library
Composer
vv4.0.0-alpha1
221stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
41/100Fair
Development Activity16
Maintenance20
Community48
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
70/100Good
Architecture70
Code Quality68
Innovation55
Learning Curve85

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 u filter, wrapping any string in a chainable UnicodeString object with dozens of Unicode-safe methods
  • The slug filter, generating locale-aware URL slugs through Symfony’s AsciiSlugger
  • The singular and plural filters, converting word forms via English, French, and Spanish inflectors
  • A single StringExtension class implementing Twig’s AbstractExtension, 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 u filter
  • 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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search