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.
Installation:
composer require danilovl/apply-filter-twig-extension-bundle
Ensure Danilovl\ApplyFilterTwigExtensionBundle\ApplyFilterTwigExtensionBundle::class is registered in config/bundles.php.
First Use Case: Apply a built-in PHP filter dynamically in Twig:
{{ apply_filter('upper', 'hello') }} {# Outputs: HELLO #}
Test with common filters like lower, trim, ucfirst, or json_encode.
tests/ directory contains practical examples of filter usage.src/DependencyInjection/ for configuration options (if any).Use apply_filter to chain or conditionally apply filters:
{% set text = ' hello world ' %}
{{ apply_filter('trim', apply_filter('lower', text)) }} {# Outputs: hello world #}
Leverage Twig’s control structures:
{% set filter = isAdmin ? 'upper' : 'lower' %}
{{ apply_filter(filter, 'test') }}
Extend the bundle by registering custom PHP filters in your Twig environment:
// src/Twig/AppExtension.php
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class AppExtension extends AbstractExtension
{
public function getFunctions(): array
{
return [
new TwigFunction('custom_filter', [$this, 'customFilter']),
];
}
public function customFilter(string $input): string
{
return strrev($input); // Example: reverse string
}
}
Then use it in Twig:
{{ apply_filter('custom_filter', 'hello') }} {# Outputs: olleh #}
Filter Availability:
strtolower, json_encode) work by default.{{ dump(apply_filter('filter_name', var)) }} to verify filter behavior.Error Handling:
Twig\Error\RuntimeError. Validate dynamically passed filter names in PHP:
if (!function_exists($filterName)) {
throw new \RuntimeException("Filter '$filterName' not found.");
}
Security:
{% set allowedFilters = ['upper', 'lower', 'trim'] %}
{% if filterName in allowedFilters %}
{{ apply_filter(filterName, text) }}
{% endif %}
strtoupper vs. upper).// config/packages/twig.yaml
twig:
debug: '%kernel.debug%'
profiler: '%kernel.debug%'
Override Default Filters: Subclass the bundle’s extension to modify or replace filters:
// src/Twig/ApplyFilterExtension.php
use Danilovl\ApplyFilterTwigExtensionBundle\Twig\ApplyFilterExtension as BaseExtension;
class ApplyFilterExtension extends BaseExtension
{
protected function getFilters(): array
{
return array_merge(parent::getFilters(), [
'custom' => [$this, 'customFilter'],
]);
}
}
Add Filter Arguments:
Extend the bundle to support filter arguments (e.g., apply_filter('substr', 'hello', 1, 2)):
// Custom extension logic
public function applyFilter(string $filter, mixed $var, array $args = []): mixed
{
if (is_callable($filter)) {
return call_user_func_array($filter, array_merge([$var], $args));
}
// Fallback to default behavior
}
config/packages/ file, meaning all behavior is runtime-driven.AppKernel registration needed.How can I help you explore Laravel packages today?