Installation:
composer require cyril-verloop/templates-bundle
For Symfony Flex projects, no additional config is required. For non-Flex projects, manually add the Twig paths in config/packages/twig.yaml:
twig:
paths:
'%kernel.project_dir%/vendor/cyril-verloop/templates-bundle/templates': CVTemplatesBundle
First Use Case: Extend a base template in your Twig file:
{% extends "@CVTemplatesBundle/base.html.twig" %}
{% block body %}
<!-- Your content here -->
{% endblock %}
Where to Look First:
templates/ directory in the package for available templates (e.g., base.html.twig, layout.html.twig).Template Inheritance: Use the provided templates as parent templates for your views:
{% extends "@CVTemplatesBundle/layout.html.twig" %}
{% block title %}My Page{% endblock %}
Customizing Blocks:
Override blocks defined in the base templates (e.g., head, footer, styles):
{% block styles %}
{{ parent() }} <!-- Include parent styles -->
<link rel="stylesheet" href="{{ asset('custom.css') }}">
{% endblock %}
Dynamic Content Injection:
Pass variables to child templates via Twig’s {% block %} or {{ include() }}:
{% block content %}
{{ include('@CVTemplatesBundle/_partials/alert.html.twig', {
type: 'success',
message: 'Operation completed!'
}) }}
{% endblock %}
Partial Reuse:
Include reusable components (e.g., @CVTemplatesBundle/_partials/navbar.html.twig) in multiple templates:
{{ include('@CVTemplatesBundle/_partials/navbar.html.twig') }}
Integration with Laravel (Symfony Bridge):
spatie/symfony) to integrate Symfony templates in Laravel.config/twig.php:
'paths' => [
base_path('vendor/cyril-verloop/templates-bundle/templates'),
],
Asset Management:
Reference assets (CSS/JS) from the bundle’s public/ directory:
<link rel="stylesheet" href="{{ asset('bundles/cvtemplates/css/base.css') }}">
Namespace Conflicts:
@CVTemplatesBundle/...) are unique in your project to avoid conflicts with other bundles or custom templates.Caching Issues:
php bin/console cache:clear
php artisan view:clear
Missing Dependencies:
twig/twig is installed via Composer:
composer require twig/twig
Symfony Flex vs. Non-Flex:
twig.yaml configuration. Forgetting this will result in TemplateNotFoundException.Laravel-Specific Quirks:
mix/vite) may not resolve paths in the bundle’s public/ directory by default. Use asset() with full paths:
{{ asset('bundles/cvtemplates/js/script.js') }}
Template Not Found:
twig.yaml matches the bundle’s templates/ directory.@CVTemplatesBundle/base.html.twig vs. @CVTemplatesBundle/base.html.twig).Block Override Failures:
{{ dump(_context.getBlockName()) }} to debug block names.Variable Scope:
{{ dump(_context.getBlockParent()) }} to inspect parent context.Customizing Templates:
templates/ directory (e.g., templates/base.html.twig). Changes here take precedence.Adding New Templates:
CVTemplatesBundle and adds new templates to Resources/views/.Hooking into Events:
TwigEvent::PRE_RENDER) to modify templates dynamically:
// In a Symfony service or Laravel event listener
$twig->addExtension(new class extends \Twig\Extension\AbstractExtension {
public function onPreRender(\Twig\Event\EnvironmentEvent $event) {
$template = $event->getTemplateName();
if (str_starts_with($template, '@CVTemplatesBundle')) {
// Modify template logic here
}
}
});
Laravel-Specific Extensions:
// In Laravel
Twig::getEnvironment()->addFunction(new \Twig\TwigFunction('cv_template_partial', [$this, 'renderPartial']));
How can I help you explore Laravel packages today?