Installation Add the package via Composer:
composer require alexandrekilian/template-bundle
Register the bundle in config/app.php under providers:
AlexandreKilian\TemplateBundle\TemplateBundle::class,
Publish Assets Publish the bundle’s assets (if needed) and configuration:
php artisan vendor:publish --provider="AlexandreKilian\TemplateBundle\TemplateBundle" --tag="config"
First Use Case: Rendering Templates
Use the Template facade to render a template:
use AlexandreKilian\TemplateBundle\Facades\Template;
$content = Template::render('path/to/template.twig');
Ensure your Twig environment is configured (if not, extend the bundle’s setup).
Dynamic Template Rendering Pass variables to templates dynamically:
$data = ['title' => 'Welcome', 'items' => [1, 2, 3]];
$rendered = Template::render('template', $data);
Template Inheritance
Leverage Twig’s extends and blocks for reusable layouts:
{# base.twig #}
<html>
<body>
{% block content %}{% endblock %}
</body>
</html>
{# page.twig #}
{% extends 'base.twig' %}
{% block content %}Hello!{% endblock %}
Integration with Brix CMS Use the bundle’s CMS-specific helpers (if documented) to fetch templates by route or entity:
$template = Template::getByRoute('homepage');
'template_paths' => [
base_path('resources/views/custom'),
],
'twig' => [
'cache' => true,
],
Missing Twig Configuration
If Twig isn’t auto-configured, manually bind it in AppServiceProvider:
$this->app->singleton('twig', function ($app) {
return new \Twig\Environment(
new \Twig\Loader\FilesystemLoader($app['config']['template_paths']),
['cache' => $app['config']['twig.cache']]
);
});
Template Not Found
Ensure paths in template_paths are correct and accessible. Use absolute paths for clarity.
Circular Dependencies
Avoid circular template inheritance (e.g., A.twig extends B.twig, which extends A.twig).
Enable Twig Debug Mode:
'twig' => [
'debug' => env('APP_DEBUG', false),
],
This shows template errors with line numbers.
Log Template Paths: Temporarily log resolved paths to verify they’re correct:
Template::setPathResolver(function ($name) {
\Log::debug("Resolving template: $name");
return base_path("resources/views/$name");
});
Custom Template Resolvers Override the template resolver to fetch templates from databases or APIs:
Template::setResolver(function ($name) {
return \DB::table('templates')->where('name', $name)->first()->content;
});
Twig Extensions
Register custom Twig functions/filters in config/template.php:
'twig' => [
'extensions' => [
\App\Twig\CustomExtension::class,
],
],
Event Listeners
If the bundle dispatches events (e.g., TemplateRendering), hook into them for pre/post-processing:
Template::addListener('TemplateRendering', function ($event) {
$event->setData(array_merge($event->getData(), ['extra' => 'value']));
});
How can I help you explore Laravel packages today?