Skip to content

Content Templates in the Vue 3 Editor ​

A Vue 3 <Editor> can offer a picker of reusable HTML snippets, for example reply texts, proposal headers, or document outlines. Define them as EditorTemplateItem objects under templates in your :init binding and listen for @template-insert to run code after a user inserts one.

Snippets can contain merge tags like {{client.name}}; Step 5 of the Laravel and Inertia guide replaces them with real values after insertion.


Key Highlights ​

  • 📋 Consumer-defined content: Templates come only from init.templates.items, so the list matches your app.
  • 🔍 Searchable picker: Filter by label or description, or switch between tabs built from each item's group.
  • 👁️ Sanitized preview: See how a template renders before it lands in the document.
  • 🎯 Caret-aware insert: The snippet is placed at the saved selection and the new HTML flows into v-model.

Configuration and Item Properties ​

Define template items in init.templates:

ts
import type { EditorInit, EditorTemplateItem } from '@erag/text-editor-vue';

const templateItems: EditorTemplateItem[] = [
    {
        id: 'message-received',
        label: 'Message Received',
        description: 'Confirm that a request was received.',
        group: 'Quick Replies',
        content:
            '<p>Hi {{client.name}},</p><p>We received your message and will get back to you shortly.</p>',
    },
    {
        id: 'progress-update',
        label: 'Progress Update',
        description: 'Share a brief progress update with the client.',
        group: 'Quick Replies',
        content:
            '<p>Hi {{client.name}},</p>' +
            '<p>Just a quick note to let you know we are working on your request.</p>' +
            '<p>Best regards,<br>{{consultant.name}}</p>',
    },
    {
        id: 'hero-with-link',
        label: 'Hero Header Block',
        description: 'A reusable introduction block with call to action link.',
        group: 'Hero Blocks',
        content:
            '<h2>Welcome, {{client.name}}</h2>' +
            '<p>Your proposal is ready for review.</p>' +
            '<p><a href="/proposals">Review Proposal</a></p>',
    },
];

const editorConfig: EditorInit = {
    templates: {
        enabled: true,
        items: templateItems,
    },
};
OptionTypeDefaultDescription
enabledbooleantrueEnables an object-based template configuration.
itemsEditorTemplateItem[][]Consumer-owned templates shown by the picker.

No starter templates are bundled. The Templates menu appears only when the feature is enabled and at least one item is configured.

Template Item Interface (EditorTemplateItem) ​

PropertyTypeRequiredDescription
idstring | numberYesStable identifier for the template item.
labelstringYesDisplay title shown in the list and dialog.
contentstringYesHTML content string inserted into the editor.
groupstringOptionalCategory name for group tab filtering (e.g. Quick Replies, Hero Blocks).
descriptionstringOptionalShort summary description shown under the label.

Interactive Template Modal Dialog ​

Enabling templates adds a dynamic top-level Templates menubar entry when configured items exist. If menubar is an explicit array, include 'templates' at the desired position, as shown in menubar customization.

Opening the modal presents:

  1. Search Bar: Instant filter by template name, description, or group.
  2. Category Tabs: Filter templates by specified group names.
  3. Live Preview Frame: Displays how the template's HTML will render once inserted.
  4. Insert Button: Inserts the selected template at the cursor location and closes the modal.

Template Events ​

Listen to the template-insert event to trigger application behavior whenever a user inserts a template:

vue
<script setup lang="ts">
import { shallowRef } from 'vue';
import { Editor, type TemplateInsertEvent } from '@erag/text-editor-vue';

const content = shallowRef('');

function handleTemplateInsert(event: TemplateInsertEvent) {
    console.log('Inserted template ID:', event.item.id);
    console.log('Inserted template label:', event.item.label);
}
</script>

<template>
    <Editor
        v-model="content"
        :init="editorConfig"
        @template-insert="handleTemplateInsert"
    />
</template>