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.
String utilities (e.g., slugify, titlecase, ascii, uppercase, lowercase, camelcase, snake_case). This aligns well with Laravel’s Blade templating system, which is Twig-compatible via packages like laravel-twig. If Blade is the primary templating engine, the fit is indirect but feasible via a wrapper layer.String component (v6.x+). Laravel’s ecosystem is PHP-FIG compliant but may require explicit version pinning to avoid conflicts with existing Symfony components (e.g., in Lumen or Symfony-based Laravel packages).// In a service provider:
Blade::directive('slugify', function ($expression) {
return "<?php echo \\Symfony\\Component\\String\\slugify({$expression}); ?>";
});
Usage in Blade:
<a href="/{{ slugify($post->title) }}">{{ $post->title }}</a>
String component is installed (or via require in composer.json).laravel-twig) or a global helper (if using Blade). Example for Twig:
$twig->addExtension(new \Twig\Extra\String\StringExtension());
String to a compatible version (e.g., ^6.0).platform-check in composer.json to enforce version constraints.php artisan view:clear).{{ slugify(item) }} for 10,000 items) could impact rendering time. Benchmark critical paths.String component is actively maintained. Monitor for breaking changes in Symfony 7.x+.String component evolves (e.g., new methods, deprecations)?// app/Helpers/StringHelper.php
use Symfony\Component\String\UnicodeString;
if (!function_exists('slugify')) {
function slugify(string $string): string {
return UnicodeString::from($string)->slugify();
}
}
Register in AppServiceProvider:
require app_path('Helpers/StringHelper.php');
laravel-twig:
// config/twig.php
'extensions' => [
\Twig\Extra\String\StringExtension::class,
],
Str::slug() in Laravel) that could be replaced.Str::slug($title) with {{ slugify($title) }} in a non-critical view.app(UnicodeString::class)->slugify()).@deprecated in PHPDoc).String v6.x). Laravel 10+ is compatible.symfony/string:^6.0.composer why symfony/string).StringExtension.LIKE clauses) aren’t duplicated in templates.composer.json:
"require": {
"symfony/string": "^6.0",
"twig/string-extra": "^1.0"
}
composer update.null inputs, Unicode 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.slugify() gains a new parameter, update the directive.symfony/string to composer.json replace or conflict sections if version locking is critical.Undefined directive 'slugify') may require checking Blade cache or directive registration.{{ dump(slugify($var)) }} for debugging in Twig/Blade.if (!function_exists('slugify')) {
function slugify(string $string): string {
return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $string)));
}
}
{{ titlecase($name) }} instead of ucwords()").@cache(md5($var), 3600)
How can I help you explore Laravel packages today?