Skip to main content

Templates

The Templates module stores reusable content templates.

At the moment the module supports HTML templates. Rendering is intentionally left to consumers.

Install

composer require qore-next/templates

The package auto-discovers TemplatesServiceProvider.

Publish migrations:

php artisan vendor:publish --tag=qore-next.templates.db
php artisan migrate

The module also has publishable config:

php artisan vendor:publish --tag=qore-next.templates.config

Data Model

Template stores:

  • name;
  • editable unique code_name, generated from name in the resource form;
  • type, cast to TemplateType;
  • optional variable_set;
  • optional description;
  • content.

The only supported type for now is TemplateType::HTML.

Variable Sets

Variable sets let application code define the Twig variables that are available for a template. A set has a code, a label, and one or more variables. Each variable can also define how it should be resolved later when rendering.

Create a variable set by extending TemplateVariableSet:

<?php

namespace App\Templates;

use Qore\Next\Templates\TemplateVariables\TemplateVariable;
use Qore\Next\Templates\TemplateVariables\TemplateVariableSet;

class MySet extends TemplateVariableSet
{
public function code(): string
{
return 'invoice';
}

public function label(): string
{
return 'Invoice';
}

public function variables(): array
{
return [
TemplateVariable::make(
key: 'test',
resolver: fn () => 'Test value'
),
TemplateVariable::make(
key: 'user.email',
resolver: fn (User $user) => $user->email,
category: 'User',
label: 'User e-mail address'
),
];
}
}

Register the set in your AppServiceProvider, next to other Qore app registrations:

use App\Templates\InvoiceTemplateVariableSet;

use function Qore\Next\Templates\Helpers\template_variable_sets;

public function register(): void
{
template_variable_sets()->register(new MySet);
}

The template resource stores the selected set in variable_set. When a set is selected, its variables are shown next to the content editor and can be inserted as raw Twig placeholders such as:

{{ invoice.number }}

Use setCategory() to group related variables in the content editor. Categories are collapsible, and variables without a category are shown under "Other".

Resource

Enable the module in Qore's modules UI. On enable it creates CRUDA permissions for templates; on disable it deletes the templates permission category.

When enabled, the module registers:

  • TemplatePolicy;
  • TemplateResource;
  • translations, config and migrations.

Register the frontend module in resources/js/app.tsx so the template icon is loaded:

import templatesModule from '../../vendor/qore-next/templates/resources/js'

<QoreAppProvider
app={{
// ...
modules: [templatesModule]
}}
/>