Skip to main content

Crate laravel_template_engine

Crate laravel_template_engine 

Source
Expand description

Templates for emails, documents and notifications that your team edits, stored in the database, for Laravel Rust.

The code declares a template type, its variables and an example; everything else (the text, the subject, the sender, who gets the email, the language) lives in the database and is changed by people without a deploy:

use laravel_template_engine::{Recipient, TemplateData};
use serde::Serialize;

#[derive(Serialize, TemplateData)]
#[template(key = "order.placed", description = "An order was placed", example = OrderPlaced::example)]
pub struct OrderPlaced {
    #[template(description = "The customer")]
    pub customer: Recipient,
    #[template(description = "The order")]
    pub order: Order,
}

#[derive(Serialize, TemplateData)]
pub struct Order {
    #[template(description = "Order number")]
    pub number: String,
    #[template(description = "Total")]
    pub total: f64,
}

impl OrderPlaced {
    fn example() -> Self {
        Self {
            customer: Recipient::new("olena@example.com").name("Olena").locale("uk"),
            order: Order { number: "1042".into(), total: 1250.0 },
        }
    }
}

laravel_template_engine::send(order).await?;

Register TemplateEngineServiceProvider and run laravel migrate. See the README for a walkthrough.

Modules§

editor
What an editor UI reads: the template types, the filters and the syntax cheat sheet.
events
What the template engine dispatches. There’s no send log in the package: a listener of these events writes one if the application needs it.
templates
What an editor needs: checking, saving, previewing and test sends.
transfer
Moving templates between environments, such as from staging to production.

Structs§

Arg
A filter argument.
Channel
A channel notification templates can write for, such as sms, slack or telegram.
Content
A template’s text in one language.
DefaultTemplate
A template a type comes with. laravel template-engine:install-defaults creates it when the type has no template of that name, so the changes people make later are never overwritten.
Document
A rendered document.
DocumentSettings
How a document is made.
Field
A named field of an object.
Filter
A filter an application adds for its templates, on top of the built-in ones.
FilterSpec
What templates may use a filter for, and how.
Font
A font PDF documents can use with font-family: name.
Image
A picture templates can show with <img src="name">.
Issue
One mistake in a template, for an editor to show next to the field.
Layout
A layout: shared HTML, such as a header, a footer and styles, that a template’s text goes into at {{ content }}.
MailAttachment
A file to attach to every email of a send, on top of the documents the templates attach. It travels through the queue, so it names a file or holds its bytes.
MailSettings
Who an email goes to and what comes with it.
Margins
The space around a PDF page’s text, in millimeters.
NotificationSettings
Which channel a notification template writes for.
NotificationTexts
The active notification templates of one type with its data, ready to render a text per channel and language. Load it before sending a Laravel notification, keep it in the notification, and render in its channel methods, which can’t wait for the database:
PendingDocument
A document from document, to adjust before it’s rendered.
PendingTemplateMail
An email send from mail.
Recipient
Someone a message can go to. A template type’s fields of this type (or Option<Recipient>, or Vec<Recipient>) are its recipient variables: a template lists them in to, cc or bcc to send to them. Templates read email, name and locale like any other fields.
RenderedMail
An email’s parts, rendered.
RenderedText
A notification’s text for one channel, rendered.
SendTemplateMail
The queued job that renders one email and sends it. Rendering happens in the worker, so the email uses the template as it is when it goes out.
Template
A stored template: an ordinary Laravel Rust model, so creating, updating and deleting one dispatches Created<Template>, Updated<Template> and Deleted<Template> for audit packages.
TemplateEngineConfig
The package’s settings, read from the environment. Every one has a default, so nothing needs to be set. An application changes them in with_config:
TemplateEngineServiceProvider
Adds the package’s settings, its migration, its command and the application’s filters.
TemplateInput
A template to save, check or preview.
TypeInfo
A template type as the engine sees it at run time.
Value
Re-export for convenience. Represents a dynamically typed value in the template engine.

Enums§

ArgKind
How a filter argument is checked when the template gives it as a constant.
DocumentFormat
The file format of a document.
InputKind
What a filter’s input should be, for the checks.
Kind
What a template makes.
Node
The shape of a value a template can read.
Orientation
The paper’s orientation.
Settings
The settings of a template, by its kind.
TemplateError
Something the template engine can’t do.

Traits§

Describe
A type that can describe its shape to the template engine. Derive it with TemplateData for your own structs.
TemplateType
A template type: what one kind of event, such as order.placed, gives its templates. Derive it with TemplateData and #[template(key = "...", example = ...)]; the engine finds derived types on its own.

Functions§

document
Makes a document from data with its type’s active document template.
find_type
The template type with key.
mail
An email send to adjust before it goes, for example with attachments.
notification
Loads the active notification templates of data’s type.
send
Sends the emails of every active email template of data’s type, through the queue. Each address in a template’s to gets its own email in its own language. Returns how many emails were queued.
type_of
The template type T.
types
Every template type in the application, sorted by key.

Derive Macros§

TemplateData
Describes a struct’s fields to the template engine. With key, the struct is also a template type that the engine finds on its own. See the laravel-template-engine crate.