dmytrof/template-parameters-bundle
Installation:
composer require dmytrof/template-parameters-bundle
Add to config/bundles.php:
Dmytrof\TemplateParametersBundle\DmytrofTemplateParametersBundle::class => ['all' => true],
First Use Case:
Replace template parameters dynamically in Twig templates.
Define parameters in config/packages/dmytrof_template_parameters.yaml:
dmytrof_template_parameters:
parameters:
app_name: "MyApp"
app_version: "1.0.0"
Use in Twig:
<title>{{ app_name }} - v{{ app_version }}</title>
Where to Look First:
config/packages/dmytrof_template_parameters.yaml (configuration)src/Controller/ (for dynamic parameter injection)templates/ (Twig usage examples)Static Parameters: Define in YAML/config:
dmytrof_template_parameters:
parameters:
site_copyright: "© {{ current_year }} MyApp"
Use in Twig:
<footer>{{ site_copyright }}</footer>
Dynamic Parameters via Controller:
Inject TemplateParameters service:
use Dmytrof\TemplateParametersBundle\Service\TemplateParameters;
public function index(TemplateParameters $templateParameters)
{
$templateParameters->set('user_name', $this->getUser()->getName());
return $this->render('index.html.twig');
}
Use in Twig:
<h1>Welcome, {{ user_name }}!</h1>
Parameter Inheritance: Extend base parameters in child templates:
# config/packages/child_template_parameters.yaml
dmytrof_template_parameters:
extends: "@dmytrof_template_parameters:parameters.yaml"
parameters:
child_param: "Overridden!"
Environment-Specific Parameters:
Use Symfony’s %env% or %kernel.environment%:
dmytrof_template_parameters:
parameters:
debug_mode: "%kernel.debug%"
kernel.request events.TemplateParameters to services for reusable logic.Caching:
php bin/console cache:clear) after changes.debug: false in config to disable auto-reload (for production).Namespace Conflicts:
app, loop).YAML Syntax:
parameters.yaml."© 2023").Dynamic Overrides:
has() to check existence:
{% if templateParameters.has('dynamic_param') %}
{{ dynamic_param }}
{% else %}
Fallback value
{% endif %}
php bin/console debug:twig
twig:
globals:
all_template_params: "@=dmytrof_template_parameters.getAll()"
Use in Twig:
<pre>{{ dump(all_template_params) }}</pre>
Custom Parameter Sources:
Implement ParameterProviderInterface to fetch parameters from APIs/DB:
class ApiParameterProvider implements ParameterProviderInterface
{
public function getParameters(): array
{
return ['api_data' => file_get_contents('https://api.example.com')];
}
}
Register in services.yaml:
Dmytrof\TemplateParametersBundle\Service\TemplateParameters:
arguments:
$parameterProviders: ['@api_parameter_provider']
Twig Runtime Overrides:
Extend the TemplateParametersExtension to add custom logic:
use Dmytrof\TemplateParametersBundle\Twig\TemplateParametersExtension as BaseExtension;
class CustomTemplateParametersExtension extends BaseExtension
{
public function getFunctions()
{
return [
new \Twig\TwigFunction('format_param', [$this, 'formatParameter']),
];
}
public function formatParameter($param, $format)
{
return strtoupper($param) . " [$format]";
}
}
Register as a Twig extension.
Parameter Validation: Add validation via Symfony’s validator:
dmytrof_template_parameters:
parameters:
email: "user@example.com"
validation:
email: "Symfony\Component\Validator\Constraints\Email"
(Requires custom implementation; see issue #X for updates.)
How can I help you explore Laravel packages today?