twig/string-extra
Twig extension integrating Symfony String: add filters u (UnicodeString methods), slug (AsciiSlugger), and singular/plural (Inflector) to manipulate text, generate slugs, and handle basic inflection directly in Twig templates.
laravel-twig or custom Blade directives. Laravel’s Blade engine is not natively compatible, requiring a wrapper layer (e.g., directives or helpers). This introduces indirect fit but remains viable for projects already using Twig or needing templating extensions.String component (v6.x+), which may conflict with Laravel’s default stack (e.g., symfony/console, symfony/http-foundation). Risk is mitigated by version pinning and composer platform checks.Str helper or custom services may be preferable.Blade::directive('slugify', function ($expression) {
return "<?php echo \\Symfony\\Component\\String\\UnicodeString::from({$expression})->slug(); ?>";
});
Feasibility: High for simple use cases (e.g., {{ slugify($title) }}), but complex operations (e.g., nested filters) may break Blade’s parsing.laravel-twig with minimal setup:
$twig->addExtension(new \Twig\Extra\String\StringExtension());
Feasibility: High, with native Twig syntax support.composer why symfony/string to detect conflicts.composer.json:
"require": {
"symfony/string": "^6.0",
"twig/string-extra": "^3.24"
},
"conflict": {
"symfony/*": "6.0.*"
}
{{ slugify($var) }} failing if $var is null.{{ truncate(item.description) }} for 10,000 items) could degrade rendering time. Benchmark with laravel-debugbar.String API evolves (e.g., new methods, deprecations).Str::slug(), ucwords())? What’s the ROI of centralizing these in templates?null values)?String component changes?String methods.
Example directive:
// app/Providers/BladeServiceProvider.php
public function boot() {
Blade::directive('slugify', function ($expression) {
return "<?php echo \\Symfony\\Component\\String\\UnicodeString::from({$expression})->slug(); ?>";
});
}
Usage:
<a href="/{{ slugify($post->title) }}">{{ $post->title }}</a>
String methods as global functions:
// app/Helpers/StringHelper.php
use Symfony\Component\String\UnicodeString;
if (!function_exists('slugify')) {
function slugify($string) {
return UnicodeString::from($string)->slug();
}
}
Load in AppServiceProvider:
require app_path('Helpers/StringHelper.php');
laravel-twig to register the extension:
// config/twig.php
'extensions' => [
\Twig\Extra\String\StringExtension::class,
],
{{ 'Hello World' | title | slugify }}
// Livewire component
public function formatTitle() {
return UnicodeString::from($this->title)->title();
}
Str::slug(), ucwords(), custom helpers).slugify) in a non-critical template.null, Unicode, special characters).app(UnicodeString::class)->slug()).if (function_exists('old_slugify')) {
trigger_deprecation('laravel', '1.0', 'Use Symfony\\String\\UnicodeString instead.');
}
String v6.x). Laravel 10+ is compatible.symfony/string:^6.0.composer why symfony/string).LIKE queries).composer require symfony/string:^6.0 twig/string-extra:^3.24
Pin versions in composer.json to avoid conflicts.slugify, titlecase, truncate).config/twig.php.SlugifyDirectiveTest).null inputs, Unicode, special characters).{{ snake_case($var) }}).try-catch in directives).String for breaking changes (e.g., method signatures, Unicode handling).twig/string-extra if new Twig extensions are added.How can I help you explore Laravel packages today?