Installation:
composer require ccc/email-template-bundle:dev-master
Register the bundle in app/Kernel.php:
new CCC\EmailTemplateBundle\EmailTemplateBundle(),
Database Setup:
Run migrations to create the email_template table:
php bin/console doctrine:schema:update --force
Routing:
Add to config/routes.yaml:
ccc_email_template:
resource: "@CCCEmailTemplateBundle/Resources/config/routing.yaml"
prefix: /admin
First Use Case: Use the form type in a Symfony form:
$builder->add('email_template', EmailTemplateType::class, [
'required' => false,
'label' => 'Select Email Template',
]);
CRUD Operations:
Access the admin panel at /admin/email-template to manage templates via the built-in UI (list, create, edit, delete).
Form Integration: Embed the template selector in any Symfony form:
$builder->add('template', EmailTemplateType::class, [
'attr' => ['class' => 'email-template-selector'],
]);
<textarea> with the template content when selected.Dynamic Template Rendering: Fetch and render a template in a controller:
$template = $this->getDoctrine()
->getRepository(EmailTemplate::class)
->findOneBy(['slug' => 'welcome_email']);
$content = $this->renderView('CCCEmailTemplateBundle:EmailTemplate:template.html.twig', [
'template' => $template,
'variables' => ['user' => $user],
]);
Twig Integration: Extend Twig with template variables:
{% extends 'base.html.twig' %}
{% block body %}
{{ render_template('welcome_email', {user: user}) }}
{% endblock %}
(Requires registering the Twig extension; see Gotchas.)
EmailTemplate entity to add fields like subject, priority, or category.EmailTemplateType form class for client-side validation.# config/access_control.yaml
- { path: ^/admin/email-template, roles: ROLE_ADMIN }
CCCEmailTemplateBundle:EmailTemplate:select.html.twig).Twig Extension Missing:
The bundle provides a Twig extension (render_template) but does not auto-register it. Manually register it in config/packages/twig.yaml:
twig:
extensions:
- CCC\EmailTemplateBundle\Twig\EmailTemplateExtension
Database Schema:
The dev-master branch may have breaking schema changes. Backup your database before updates.
jQuery Dependency: The AJAX selector relies on jQuery. If missing, the template dropdown will fail silently. Include it in your layout:
{{ encore_entry_link_tags('app') }} {# or #}
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
Form Type Namespace:
The form type is email_template_select (not EmailTemplateType). Use:
$builder->add('template', 'email_template_select', [...]);
Translations:
Default labels (e.g., "Add Template") require the translator component. If missing, they’ll appear as keys (e.g., Add template).
AJAX Fails:
Check the browser’s Network tab for 404s on /ajax/template-content. Ensure the route is loaded and the FOSJsRoutingBundle is configured.
Template Not Found:
Verify the slug or id exists in the email_template table. Use:
php bin/console doctrine:query:sql "SELECT * FROM email_template"
Twig Errors: Clear the cache after adding the Twig extension:
php bin/console cache:clear
Custom Template Storage:
Override the EmailTemplate entity to store templates in a file system or API instead of the database.
Template Editor:
Replace the default textarea with a WYSIWYG editor (e.g., TinyMCE) by extending the select.html.twig template.
Event Listeners: Add logic before/after template updates via Symfony events:
// src/EventListener/EmailTemplateListener.php
public function onPreUpdate(PreUpdateEventArgs $args) {
$template = $args->getObject();
// Sanitize or log changes
}
Register in services.yaml:
services:
App\EventListener\EmailTemplateListener:
tags:
- { name: doctrine.event_listener, event: preUpdate }
API Endpoint: Expose templates via API by creating a custom controller:
#[Route('/api/templates', name: 'api_templates', methods: ['GET'])]
public function getTemplates(): JsonResponse {
$templates = $this->getDoctrine()
->getRepository(EmailTemplate::class)
->findAll();
return $this->json($templates);
}
How can I help you explore Laravel packages today?