danilovl/apply-filter-twig-extension-bundle
Symfony Twig bundle that adds an apply_filter() function to call Twig filters dynamically from templates. Choose filter names at runtime and apply them to values (e.g., upper/lower/max), enabling flexible rendering logic.
Pros:
max, upper, lower), useful for conditional data transformation in templates.symfony/twig-bridge) allows adoption with minimal friction.Cons:
apply_filter('trim|upper', $str)). Complex pipelines (e.g., custom PHP callbacks) may need workarounds.tightenco/ziggy + symfony/twig-bridge for Twig support. The bundle can be installed alongside these dependencies.strtoupper, array_filter) but lacks Laravel-specific helpers (e.g., Str::title).Twig\Environment with Laravel’s service container). Misconfiguration could break template rendering.apply_filter('system', 'malicious')). Requires validation layer.['upper', 'lower', 'trim'])?symfony/twig-bridge and tightenco/ziggy. Requires:
composer require symfony/twig-bridge tightenco/ziggy danilovl/apply-filter-twig-extension-bundle
apply_filter.@applyFilter('max', $array) without Twig.Str::, Arr::, or collect()->pipe() for simpler cases.apply_filter with basic filters (e.g., upper, json_encode) in a non-critical template.tideways/xhprof or Laravel Debugbar.// app/Twig/ApplyFilterExtension.php
class ApplyFilterExtension extends \Twig\Extension\AbstractExtension
{
public function getFilters(): array
{
return ['allowed_filters' => ['upper', 'lower', 'trim']];
}
}
apply_filter to reject unauthorized filters.@php echo Str::upper($str) @endphp) with {{ apply_filter('upper', str) }}.{{ apply_filter('json_encode|prettify', $data) }}).Bundle autoloading. In Laravel, manually register the bundle in config/app.php:
'extra.bundles' => [
Danilovl\ApplyFilterTwigExtensionBundle\ApplyFilterTwigExtensionBundle::class => true,
],
apply_filter may bypass Laravel’s Blade caching. Use twig.config.cache: true and test cached vs. dynamic templates.config/twig.php.composer.json and register the bundle.apply_filter('upper', 'test') === 'TEST').symfony/var-dumper to check memory/CPU impact.debug: true enabled.danilovl/apply-filter-twig-extension-bundle may diverge from Laravel’s updates.twig.config.debug: true and monolog logging.Filter "unknown" not found).apply_filter vs. @php).laravel-debugbar). Mitigate by:
{{ apply_filter('upper|trim', $str) }}).apply_filter('array_filter', $hugeArray)) may spike memory. Use collect()->filter()->toArray() as an alternative.apply_filter is thread-safe but may contend with Laravel’s queue workers if filters are CPU-intensive.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Twig misconfiguration | Blank templates or 500 errors | Use twig.config.debug: true and rollback. |
| Unauthorized filter execution | RCE or data corruption | Whitelist filters and validate inputs. |
How can I help you explore Laravel packages today?