Documentation
¶
Overview ¶
Package tmpl provides Go html/template integration for Folio.
It bridges Go's standard html/template engine with Folio's HTML-to-PDF converter, letting you define PDF templates as HTML files with Go template directives ({{.Field}}, {{range}}, {{if}}, etc.) and render them to layout elements or complete PDF documents with a single call.
This is the "third input mode" alongside Folio's Go layout API and raw HTML string input: templates live in separate files, designers can preview them in a browser, and the rendering pipeline is html/template → html.ConvertFull → layout.Element[].
All functions in this package are safe for concurrent use. User-provided Funcs override built-in helpers with the same name.
Quick start:
// Render a template string to layout elements:
data := map[string]any{"Title": "Invoice #1042", "Customer": "Acme"}
elems, err := tmpl.Render(`<h1>{{.Title}}</h1><p>Bill to: {{.Customer}}</p>`, data, nil)
// Render a template file to a complete PDF:
err := tmpl.RenderFile("invoice.html", data, nil, "invoice.pdf")
// Use custom template functions:
opts := &tmpl.Options{
Funcs: template.FuncMap{"upper": strings.ToUpper},
}
elems, err := tmpl.Render(`<p>{{upper .Name}}</p>`, data, opts)
Index ¶
- func Render(templateStr string, data any, opts *Options) ([]layout.Element, error)
- func RenderDocument(templateStr string, data any, opts *Options) (*document.Document, error)
- func RenderFile(templatePath string, data any, opts *Options, outPath string) error
- func RenderFileTo(w io.Writer, templatePath string, data any, opts *Options) error
- func RenderTo(w io.Writer, templateStr string, data any, opts *Options) error
- type Options
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Render ¶
Render executes a Go html/template string with the given data and converts the resulting HTML to Folio layout elements. This is the lowest-level entry point — use RenderDocument or RenderFile if you want a complete PDF.
func RenderDocument ¶
RenderDocument executes a template string and returns a fully laid-out Document ready for Save(). The caller can add headers, footers, or additional elements before saving.
When the template contains @page CSS rules (size, margins, margin boxes), they are applied to the document automatically.
func RenderFile ¶
RenderFile reads a template from disk, executes it with data, and writes the resulting PDF to outPath. The template's directory is used as the BaseFS for resolving relative asset references (images, fonts, stylesheets) in the HTML unless ConvertOpts.BaseFS is already set.
RenderFile never mutates the caller's Options — it clones ConvertOpts internally before defaulting BaseFS.
func RenderFileTo ¶
RenderFileTo reads a template from disk, executes it with data, and writes the resulting PDF to w. This is the natural entry point for HTTP handlers that serve PDFs directly to the response writer.
Like RenderFile, the template's directory is used as BaseFS for asset resolution unless ConvertOpts.BaseFS is already set. The caller's Options are never mutated.
Types ¶
type Options ¶
type Options struct {
// Funcs is an optional function map passed to html/template.
// Use this to register custom helpers (formatting, i18n, etc.)
// available inside {{call}} directives in the template.
// User-provided funcs override built-in helpers with the same
// name (e.g. a custom "dict" replaces the default one).
Funcs htmltpl.FuncMap
// BaseTemplate is an optional pre-parsed template tree that the
// template string is parsed into. Use this for shared partials:
//
// base := template.Must(template.ParseGlob("partials/*.html"))
// opts := &tmpl.Options{BaseTemplate: base}
// elems, _ := tmpl.Render(`{{template "header" .}}...`, data, opts)
//
// When set, the template string is parsed into a clone of
// BaseTemplate (via t.Clone().Parse()), so all {{define}} blocks
// and {{template}} calls in BaseTemplate are available. The
// original BaseTemplate is never modified.
BaseTemplate *htmltpl.Template
// ConvertOpts is passed through to html.ConvertFull. Use it to set
// page dimensions, default font size, BaseFS for assets, etc.
// RenderFile clones this before defaulting BaseFS to the template's
// directory, so the caller's original value is never mutated.
ConvertOpts *foliohtml.Options
// PageSize sets the page size for RenderFile / RenderDocument.
// Defaults to US Letter (612x792 pt). Ignored by Render, which
// returns raw elements without page context. When the template
// contains @page CSS rules, the @page size takes precedence.
PageSize document.PageSize
// Margins sets the page margins for RenderFile / RenderDocument.
// Defaults to 36pt (0.5in) on all sides. Ignored by Render.
// When the template contains @page margin rules, those take
// precedence. Use a non-nil pointer to explicitly set zero
// margins: &layout.Margins{} produces a full-bleed document.
Margins *layout.Margins
}
Options configures template rendering. All fields are optional.