Installation
Add the bundle to your composer.json:
composer require c33s-toolkit/layout-bundle
Enable it in config/bundles.php:
return [
// ...
C33S\Toolkit\LayoutBundle\C33SToolkitLayoutBundle::class => ['all' => true],
];
First Use Case Extend the base layout in your Twig templates:
{% extends 'c33s_layout_base.html.twig' %}
{% block title %}My Page Title{% endblock %}
{% block content %}
<h1>Welcome!</h1>
{% endblock %}
Key Files
templates/c33s_layout_base.html.twig: The base layout template.Resources/config/services.yaml: Configuration for layout services.Extending Layouts Override blocks in child templates:
{% extends 'c33s_layout_base.html.twig' %}
{% block header %}
{{ parent() }} <!-- Include parent block -->
<div class="custom-header">...</div>
{% endblock %}
Dynamic Layouts Use Twig logic to conditionally render sections:
{% block sidebar %}
{% if is_granted('ROLE_ADMIN') %}
<div class="admin-sidebar">...</div>
{% endif %}
{% endblock %}
Integration with Bootstrap3 Bundle
Combine with bootstrap3-bundle for consistent styling:
{% extends 'c33s_layout_base.html.twig' %}
{% block stylesheets %}
{{ parent() }}
<link rel="stylesheet" href="{{ asset('css/custom.css') }}">
{% endblock %}
Service-Based Layouts Inject layout services in controllers:
use C33S\Toolkit\LayoutBundle\Service\LayoutService;
class HomeController extends AbstractController {
public function index(LayoutService $layoutService) {
return $this->render('home/index.html.twig', [
'layout' => $layoutService->getLayoutData(),
]);
}
}
Twig Block Inheritance
Forgetting {% block %} or {% endblock %} will break layout rendering. Use {{ parent() }} to include parent block content.
Symfony 3 Compatibility The bundle is designed for Symfony 3.x. Test thoroughly in Symfony 4+ if upgrading.
Caching Issues Clear Twig cache after modifying templates:
php bin/console cache:clear --env=prod
templates/ or a configured Twig path.dump() in Twig to inspect passed variables:
{{ dump(layout) }}
Custom Layouts
Override the base template by creating a new file (e.g., templates/base_custom.html.twig) and extend it in your templates.
Layout Services
Extend LayoutService to add custom logic:
namespace App\Service;
use C33S\Toolkit\LayoutBundle\Service\LayoutService;
class CustomLayoutService extends LayoutService {
public function getCustomData() {
return ['key' => 'value'];
}
}
Configuration
Override bundle config in config/packages/c33s_toolkit_layout.yaml:
c33s_toolkit_layout:
default_layout: 'app_layouts/base.html.twig'
How can I help you explore Laravel packages today?