- What is twig/string-extra used for?
- It is a Twig extension that integrates Symfony String into templates. It registers the u, slug, singular, and plural filters so you can do Unicode-aware string handling, URL slugs, and simple inflection in Twig. It is a presentation-layer helper, not a Laravel business-logic package.
- How do I install twig/string-extra in a Laravel app?
- Install it with Composer using composer require twig/string-extra symfony/string. Then register Twig\Extra\String\StringExtension in your Twig environment configuration. This only makes sense if your Laravel app actually renders Twig through a bridge or a Symfony-style Twig service.
- Does twig/string-extra work with Laravel Blade?
- No, not as-is. The package provides Twig filters, so Blade templates cannot call them directly. Blade-first Laravel apps should use Laravel's Str::slug, Str::plural, and Str::of helpers, or write a small custom directive that wraps Symfony\Component\String\UnicodeString.
- Which Laravel versions are compatible with twig/string-extra?
- The package does not target Laravel versions directly; it is a Twig extension. Compatibility depends on your Twig integration and the symfony/string constraint that Laravel already pins. Run composer why-not symfony/string before adding it to avoid forcing a lockfile-wide Symfony upgrade.
- What filters does twig/string-extra add?
- It adds four filters: u wraps text in a Symfony UnicodeString, slug wraps AsciiSlugger::slug, and singular and plural wrap the Inflector's singularize and pluralize methods. These are intended for template-level text manipulation, not domain logic.
- Do I need symfony/string installed separately?
- Yes. twig/string-extra is a thin wrapper around Symfony String components, so symfony/string must be available. In many Laravel projects it is already present transitively, but you should verify the installed constraint before relying on it.
- How do I register the StringExtension in Twig?
- Add Twig\Extra\String\StringExtension to the extensions list of your Twig environment. In a Laravel Twig bridge, this is usually done in the Twig configuration or service provider that builds the environment. There is no package-specific config, middleware, or Laravel service provider to publish.
- Can twig/string-extra handle Unicode and multilingual text?
- The u filter gives you Symfony UnicodeString methods, and slug uses AsciiSlugger for transliteration-friendly URLs. The singular and plural filters use Symfony's Inflector and are best treated as simple English-oriented inflection helpers. Test locale-specific and non-English cases in your templates before depending on them.
- Is twig/string-extra safe to use in production?
- It is MIT-licensed, small, and maintained in step with Twig releases, with behavior inherited from Symfony String. The main risks are version coupling to symfony/string and thin community adoption around the Twig wrapper itself. Add template-level regression tests for Unicode, slugs, inflection, and empty strings.
- What are alternatives to twig/string-extra in Laravel?
- For Blade, use Laravel's native Str::slug, Str::plural, and Str::of methods. For Twig, you can register Symfony String directly in a custom extension or call UnicodeString from a Twig function. If you only need URL slugs, Laravel's Str::slug is usually the simpler choice.