render

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package render provides themed email generation with CSS inlining, automatic plain text derivation, and markdown support

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTemplateParsingFailed is returned when a Go template fails to parse
	ErrTemplateParsingFailed = errors.New("template parsing failed")

	// ErrTemplateExecutionFailed is returned when a Go template fails to execute
	ErrTemplateExecutionFailed = errors.New("template execution failed")

	// ErrCSSInliningFailed is returned when the premailer CSS inlining transform fails
	ErrCSSInliningFailed = errors.New("css inlining failed")

	// ErrPlainTextConversionFailed is returned when html-to-plaintext conversion fails
	ErrPlainTextConversionFailed = errors.New("plain text conversion failed")

	// ErrThemeRequired is returned when a Renderer is created or invoked with a nil theme
	ErrThemeRequired = errors.New("theme is required")
)

Functions

func Bold

func Bold(s string) template.HTML

Bold wraps text in a <strong> tag with HTML-escaped content

func ExecuteTextTemplate

func ExecuteTextTemplate(name, tmplStr string, data map[string]any) (string, error)

ExecuteTextTemplate parses and executes a text/template string with sprig template functions against the provided data map

func HTMLToPlainText

func HTMLToPlainText(html string) (string, error)

HTMLToPlainText converts HTML content to a plain text representation with pretty-printed tables using go-premailer's built-in text conversion

func InlineCSS

func InlineCSS(html string) (string, error)

InlineCSS transforms CSS style blocks into inline style attributes for email client compatibility using premailer

func Link(href, text string) template.HTML

Link renders an anchor tag with HTML-escaped href and text using the default link color

func LinkWithColor

func LinkWithColor(href, text, color string) template.HTML

LinkWithColor renders an anchor tag with HTML-escaped href, text, and a custom inline color

Types

type Action

type Action struct {
	// Instructions is the descriptive text displayed above the action button or invite code
	Instructions string
	// Button holds the CTA button details
	Button Button
	// InviteCode is an invite or authentication code displayed instead of a button
	InviteCode string
	// Style is per-Action style overrides
	Style Style
}

Action is a call-to-action element in the body — a CTA button or an invite code

type Button

type Button struct {
	// Text is the button label
	Text string
	// Link is the URL the button navigates to
	Link string
	// Color is the hex background color; convenience override of Style.ButtonColor
	Color string
	// TextColor is the hex label color; convenience override of Style.ButtonTextColor
	TextColor string
	// Style is per-Button style overrides
	Style Style
}

Button is a clickable CTA button rendered in an Action

type Callout

type Callout struct {
	// Title is the heading displayed above the list items
	Title string
	// Items is the list of entries rendered without escaping; use render.Link / render.Bold for inline formatting
	Items []template.HTML
	// Ordered renders a numbered list when true; unordered (bulleted) when false
	Ordered bool
	// Style is per-Callout style overrides
	Style Style
}

Callout is a visually distinct block for bulleted or numbered lists with an optional heading

type Cell

type Cell struct {
	// Key is the column or property name
	Key string
	// Value is the text value, HTML-escaped during rendering
	Value string
	// UnsafeValue is a raw HTML value used when Value is empty
	UnsafeValue template.HTML
}

Cell is a single key-value entry used in dictionaries and table cells

type ContentBody

type ContentBody struct {
	// Preheader is hidden text shown as the email preview in email clients
	Preheader string
	// Header is the top header row rendered by modern themes
	Header HeaderBlock
	// Icon is an image displayed between the logo and content area
	Icon *ContentIcon
	// Name is the recipient name used in the greeting line
	Name string
	// Greeting is the greeting text preceding the recipient name
	Greeting string
	// Title overrides the greeting and name when set, used as the full heading line
	Title string
	// Intros is the introductory block rendered before the main content
	Intros IntrosBlock
	// ContentBlocks are standalone raw HTML blocks rendered after intros and before structured content
	ContentBlocks []template.HTML
	// Callout is a visually distinct block for bulleted or numbered lists with an optional heading
	Callout *Callout
	// Dictionary is a block of key-value pairs rendered as a definition list
	Dictionary Dictionary
	// Tables contains data tables rendered in the body
	Tables []DataTable
	// Actions contains call-to-action buttons and invite codes
	Actions []Action
	// Outros is the closing block rendered after the main content
	Outros OutrosBlock
	// Signature is the sign-off text (e.g. "Best regards")
	Signature string
	// SignatureName is the name appended under the signature line
	SignatureName string
	// FreeMarkdown is a markdown block that replaces all structured body content when set
	FreeMarkdown MarkdownContent
	// BodyWidth overrides the email body width (e.g. "600px")
	BodyWidth string
	// AdditionalCSS is raw CSS appended to the theme styles
	AdditionalCSS string
	// Style is per-Body style overrides
	Style Style
}

ContentBody is the per-send structured body content; templates reference only the slots they render

type ContentIcon

type ContentIcon struct {
	// Src is the URL of the icon image
	Src string
	// Alt is the alt text for the icon image
	Alt string
}

ContentIcon is an icon image displayed between the logo and content area

type DataTable

type DataTable struct {
	// Title is the heading displayed above the table
	Title string
	// TitleUnsafe is a raw HTML title that overrides Title when non-empty
	TitleUnsafe template.HTML
	// Data is a 2D array of cells representing the table rows and columns
	Data [][]Cell
	// Columns holds column width and alignment configuration
	Columns TableColumns
	// Class is the CSS class applied to the table wrapper element
	Class string
	// Footer is the text displayed below the table
	Footer string
	// FooterUnsafe is a raw HTML footer that overrides Footer when non-empty
	FooterUnsafe template.HTML
	// Style is per-DataTable style overrides
	Style Style
}

DataTable is a data table rendered in the body

type Dictionary

type Dictionary struct {
	// Cells is the list of key-value entries
	Cells []Cell
	// Style is per-Dictionary style overrides
	Style Style
}

Dictionary is a block of key-value cells rendered as a definition list

type EmailContent

type EmailContent struct {
	// Request is the operation input value, typed at the dispatch site
	// Templates reach its fields via reflection (e.g. .Request.FirstName, .Request.CampaignID)
	Request any
	// Config is the per-send config supplied by the dispatcher
	Config any
	// Body is the per-send structured body content
	Body ContentBody
}

EmailContent is the root object rendered against a theme template. Templates reference its fields via dotted paths — {{ .Body.Title }}, {{ .Config.CompanyName }} style overrides are attached to the section that actually renders them (e.g. Body.Style, Body.Intros.Style)

type HeaderBlock

type HeaderBlock struct {
	Logo *ContentIcon
	// Style is per-HeaderBlock style overrides
	Style Style
}

HeaderBlock is the top header row rendered by modern themes; carries the compact brand logo and per-section style

type IntrosBlock

type IntrosBlock struct {
	// Paragraphs is the list of plain-text paragraphs rendered in order
	Paragraphs []string
	// Markdown is a markdown intro block convertible to HTML via goldmark
	Markdown MarkdownContent
	// Unsafe contains raw HTML intro blocks trusted by the template author
	Unsafe []template.HTML
	// Style is per-Intros style overrides
	Style Style
}

IntrosBlock is the introductory paragraph block. Templates pick one of Paragraphs, Markdown, or Unsafe depending on the intended escape path

type MarkdownContent

type MarkdownContent template.HTML

MarkdownContent is markdown source text convertible to HTML via goldmark with GFM and table extensions. The ToHTML method is safe to call on the zero value

func (MarkdownContent) ToHTML

func (m MarkdownContent) ToHTML() template.HTML

ToHTML converts the markdown content to HTML using goldmark with GFM and table extensions

type OutrosBlock

type OutrosBlock struct {
	// Paragraphs is the list of plain-text paragraphs rendered in order
	Paragraphs []string
	// Markdown is a markdown outro block convertible to HTML via goldmark
	Markdown MarkdownContent
	// Unsafe contains raw HTML outro blocks trusted by the template author
	Unsafe []template.HTML
	// Style is per-Outros style overrides
	Style Style
}

OutrosBlock is the closing paragraph block. Templates pick one of Paragraphs, Markdown, or Unsafe depending on the intended escape path

type Renderer

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

Renderer generates themed HTML and plain text email output from an EmailContent value

func NewRenderer

func NewRenderer(opts ...RendererOption) *Renderer

NewRenderer creates a Renderer with the given options applied over sensible defaults

func (*Renderer) GenerateHTML

func (r *Renderer) GenerateHTML(content EmailContent) (string, error)

GenerateHTML renders the EmailContent into a complete HTML string using the configured theme. The content value is passed directly as the template root context, so template authors reference fields via flat dotted paths (e.g. {{ .Body.Title }}, {{ .Config.LogoURL }})

func (*Renderer) GeneratePlainText

func (r *Renderer) GeneratePlainText(content EmailContent) (string, error)

GeneratePlainText renders the EmailContent into a plain text string using the configured theme

type RendererOption

type RendererOption func(*Renderer)

RendererOption is a function that configures a Renderer

func WithTheme

func WithTheme(t *Theme) RendererOption

WithTheme sets the theme used for email rendering

type Style

type Style struct {
	// PrimaryColor is the headline/emphasis color
	PrimaryColor string
	// SecondaryColor is the secondary accent color
	SecondaryColor string
	// BackgroundColor is the outer page background color
	BackgroundColor string
	// TextColor is the body text color
	TextColor string
	// ButtonColor is the call-to-action button background color
	ButtonColor string
	// ButtonTextColor is the call-to-action button text color
	ButtonTextColor string
	// LinkColor is the anchor link color
	LinkColor string
	// FontFamily is the CSS font-family stack
	FontFamily string
}

Style holds color and font overrides used throughout the email. Every section embeds a Style so template authors can override look per section; zero-value fields fall through to whatever fallback the theme template elects

type TableColumns

type TableColumns struct {
	// CustomWidth maps column keys to width values (e.g. "Price": "15%")
	CustomWidth map[string]string
	// CustomAlignment maps column keys to alignment values (e.g. "Price": "right")
	CustomAlignment map[string]string
}

TableColumns holds column-level display configuration for a DataTable

type Theme

type Theme struct {
	// Name is the human-readable identifier for the theme
	Name string
	// HTML is the raw Go html/template string for the theme
	HTML string
	// Text is the raw Go text/template string for the theme
	Text string
}

Theme holds the HTML and plain text template strings for an email theme. Theme authors reference EmailContent fields directly in the template via dotted paths (e.g. {{ .Body.Title }}, {{ .Config.LogoURL }}); any section- level Style override precedence is expressed inline in the template

Jump to

Keyboard shortcuts

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