Skip to content

TemplatesModule ​

This module adds a Templates button to the toolbar. When clicked, it shows a dropdown listing predefined HTML templates. Selecting a template inserts its content at the current cursor position in the editor.

Requirements ​

Both TemplatesField in quill_options and TemplatesModule in modules are required:

php
'quill_options' => [
    [new TemplatesField()],
],
'modules' => [
    new TemplatesModule(),
],

TemplatesField declares the button in the toolbar. TemplatesModule configures the templates and powers the dropdown. Because TemplatesField auto-imports TemplatesModule, you only need to add TemplatesModule explicitly when you want to pass custom templates.

Default templates ​

The following templates are provided out of the box and can be used as-is or as a reference:

ConstantLabelDescription
TemplatesModule::TEMPLATE_SIGNATURESignatureClosing block with name, title and email placeholders
TemplatesModule::TEMPLATE_INTRODUCTIONIntroductionFormal opening paragraph
TemplatesModule::TEMPLATE_BULLET_LISTBullet listUnordered list with three placeholder items
TemplatesModule::TEMPLATE_TABLETable (3×3)HTML table with a header row and two data rows

You can mix default constants with your own templates:

php
new TemplatesModule(options: [
    TemplatesModule::TEMPLATE_SIGNATURE,
    TemplatesModule::TEMPLATE_INTRODUCTION,
    ['label' => 'My custom template', 'content' => '<p>Custom content here</p>'],
])

Options ​

TemplatesModule accepts an array of templates. Each template is an associative array with two keys:

KeyTypeDescription
labelstringThe name displayed in the dropdown
contentstringThe HTML inserted into the editor at the cursor position

Basic usage ​

php
use Ehyiah\QuillJsBundle\DTO\Fields\InlineField\TemplatesField;
use Ehyiah\QuillJsBundle\DTO\Modules\TemplatesModule;
use Ehyiah\QuillJsBundle\Form\QuillType;

$builder->add('content', QuillType::class, [
    'quill_options' => [
        [new BoldField(), new ItalicField(), new TemplatesField()],
    ],
    'modules' => [
        new TemplatesModule(options: [
            ['label' => 'Signature', 'content' => '<p>Cordialement,<br>John Doe</p>'],
            ['label' => 'Introduction', 'content' => '<p>Bonjour,</p><p>Je me permets de vous contacter au sujet de...</p>'],
            ['label' => 'Legal notice', 'content' => '<p><em>This document is confidential and intended solely for the addressee.</em></p>'],
        ]),
    ],
]);

Use default templates without customization ​

Simply declare TemplatesModule without arguments:

php
'quill_options' => [
    [new TemplatesField()],
],
'modules' => [
    new TemplatesModule(),
],

Behaviour ​

  • The Templates button is visible in the toolbar once TemplatesField is added to quill_options.
  • Clicking the button opens a dropdown listing all configured templates.
  • Clicking a template item inserts its HTML content at the current cursor position without replacing existing content.
  • The dropdown closes automatically when clicking outside the editor or after a template is selected.
  • The editor focus and cursor position are preserved during template insertion.