difane/difane-twig-database-bundle
Installation Add the bundle via Composer:
composer require difane/difane-twig-database-bundle
Enable it in config/bundles.php:
return [
// ...
Difane\TwigDatabaseBundle\DifaneTwigDatabaseBundle::class => ['all' => true],
];
Configuration Publish the default config:
php bin/console difane:twig-database:install
Update config/packages/difane_twig_database.yaml to define:
template_paths (database paths, e.g., ['app.templates']).loader settings (e.g., cache: true).First Use Case Store a template in the database:
php bin/console difane:twig-database:create app.templates/hello.html.twig
Edit it via the CLI:
php bin/console difane:twig-database:edit app.templates/hello.html.twig
Use it in Twig:
{% include 'app.templates/hello.html.twig' %}
Hybrid Storage
templates/base.html.twig).config/packages/difane_twig_database.yaml:
difane_twig_database:
template_paths:
- '%kernel.project_dir%/templates' # Filesystem
- 'app.templates' # Database
Template Management
# Create a new template
php bin/console difane:twig-database:create app.templates/newsletter.twig
# Edit existing template
php bin/console difane:twig-database:edit app.templates/newsletter.twig
// In a service
$this->twigDatabaseManager->updateTemplate('app.templates/home.twig', $newContent);
Integration with Twig
Loader to prioritize database templates:
// config/packages/twig.yaml
twig:
loader:
difane_twig_database:
paths: ['app.templates']
{% extends 'app.templates/base.html.twig' %}
{% block content %}{% include 'app.templates/partials/header.twig' %}{% endblock %}
Dynamic Template Rendering
$template = $this->twigDatabaseManager->getTemplate('app.templates/article.twig');
$html = $this->twig->render($template->getContent(), ['title' => 'Post']);
admin.dashboard, user.profile) to avoid collisions.cache: true in config to avoid repeated DB queries.Loader Conflicts
home.html.twig), the filesystem path takes precedence.db.templates/home.html.twig vs. fs.templates/home.html.twig).Case Sensitivity
App.Templates/HELLO.twig ≠ app.templates/hello.twig).CLI Permissions
difane:twig-database:edit command may fail if the database user lacks write permissions.INSERT, UPDATE, DELETE on the twig_template table:
GRANT ALL PRIVILEGES ON twig_template TO your_db_user;
Caching Quirks
cache: true causes performance degradation with frequent DB queries.$this->cache->get('template:app.templates/home.twig', function() {
return $this->twigDatabaseManager->getTemplate('app.templates/home.twig')->getContent();
});
Template Not Found
twig_template table for the correct path. Run:
php bin/console difane:twig-database:list
Loader Errors
config/packages/dev/twig.yaml:
twig:
debug: true
strict_variables: true
DifaneTwigDatabaseBundle is loaded before other Twig bundles.Migration Issues
twig_template table is missing, run:
php bin/console doctrine:migrations:migrate
Custom Template Storage
Difane\TwigDatabaseBundle\Manager\TemplateManager to support additional storage backends (e.g., S3):
class CustomTemplateManager extends TemplateManager {
public function getTemplate($path) {
// Add logic for S3 fallback
return parent::getTemplate($path);
}
}
config/services.yaml:
services:
Difane\TwigDatabaseBundle\Manager\TemplateManager:
class: App\Service\CustomTemplateManager
Event Listeners
TemplateCreatedEvent) to log changes or notify users:
// src/EventListener/TemplateListener.php
class TemplateListener implements TemplateEvents {
public function onTemplateCreated(TemplateCreatedEvent $event) {
// Send email or log the change
}
}
config/services.yaml:
services:
App\EventListener\TemplateListener:
tags:
- { name: 'kernel.event_listener', event: 'difane.twig_database.template.created' }
Custom CLI Commands
php bin/console app:twig-database:import /path/to/templates --namespace=app.templates
class ImportTemplatesCommand extends ContainerAwareCommand {
protected function execute(InputInterface $input, OutputInterface $output) {
// Logic to import templates from filesystem to DB
}
}
How can I help you explore Laravel packages today?