Help us shape the future of Go! Take the 2026 Go Developer Survey

emailtemplate

package
v1.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 5, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package emailtemplate is the hand-rolled variable-interpolation engine for user email templates (beta). It deliberately implements a flat subset of Mustache — `{{ident}}` (escaped under EscapeHTML) and `{{{ident}}}` (raw) — with NO third-party dependency, and reserves Mustache's structural syntax (`{{#…}}`, `{{/…}}`, `{{^…}}`, `{{>…}}`, `{{!…}}`) as a parse error so a future upgrade to sections/partials/comments stays purely additive: no template that parses today can change meaning later.

The API is two-phase — Parse then Render — so the create/validate endpoints can reject bad syntax without any data, and the send path can render one parsed part against caller-supplied template_data. Missing variables render as the empty string (the permissive Postmark model); a path that resolves to an object or array also renders empty.

Index

Constants

View Source
const (
	// MaxSourceBytes caps one template part's source at Parse time.
	MaxSourceBytes = 256 * 1024
	// MaxDataDepth caps template_data nesting (maps/arrays) at Render time.
	MaxDataDepth = 8
	// MaxRenderedBytes caps one rendered part at Render time.
	MaxRenderedBytes = 2 * 1024 * 1024
)

Caps. Enforced here so every caller (create, validate, send) shares one definition; the outbound path re-validates rendered output independently.

Variables

This section is empty.

Functions

This section is empty.

Types

type EscapeMode

type EscapeMode int

EscapeMode selects how `{{ident}}` output is encoded. `{{{ident}}}` is always raw.

const (
	// EscapeNone renders both `{{x}}` and `{{{x}}}` verbatim — used for the
	// subject and plain-text body, where HTML entities would be garbage.
	EscapeNone EscapeMode = iota
	// EscapeHTML HTML-escapes `{{x}}` (and leaves `{{{x}}}` raw) — used for
	// html_body so interpolated user data can't inject markup by default.
	EscapeHTML
)

type ParseError

type ParseError struct {
	Offset  int
	Message string
}

ParseError describes a syntax error with its byte offset in the source.

func (*ParseError) Error

func (e *ParseError) Error() string

type Template

type Template struct {
	// contains filtered or unexported fields
}

Template is a parsed template part, safe for concurrent Render calls.

func Parse

func Parse(src string) (*Template, error)

Parse compiles a template part. It fails on source over MaxSourceBytes, unclosed tags, empty tags, invalid identifiers, and reserved (Mustache structural) syntax.

func (*Template) Render

func (t *Template) Render(data map[string]any, escape EscapeMode) (string, error)

Render interpolates data into the template. Missing variables (and paths resolving to objects/arrays) render as ""; scalars render naturally (see formatScalar). It fails when data nests deeper than MaxDataDepth or the output exceeds MaxRenderedBytes.

func (*Template) Vars

func (t *Template) Vars() []string

Vars returns the template's identifiers in first-appearance order, deduped. A template with no tags yields nil.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL