Documentation
¶
Overview ¶
Package mist renders a strict subset of Liquid in a single pass, bailing with ErrUnsupported on anything outside the subset so the caller can fall back to a full engine. See SPEC.md for the exact subset.
Index ¶
- Variables
- func Append(dst []byte, tpl string, vars map[string]any, strict bool) ([]byte, error)
- func Check(tpl string) error
- func Render(tpl string, vars map[string]any, strict bool) (string, error)
- type Constructs
- type Dialect
- type Engine
- func (e Engine) Append(dst []byte, tpl string, vars map[string]any, strict bool) (out []byte, err error)
- func (e Engine) Check(tpl string) (err error)
- func (e Engine) Render(tpl string, vars map[string]any, strict bool) (string, error)
- func (e Engine) RenderChain(steps []Step, vars map[string]any) (res []Result, n int, hydrated map[string]any)
- type Error
- type Filter
- type FilterFunc
- type Keyword
- type Result
- type Step
- type Tag
- type TagFunc
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUnsupported means the template is outside the subset; render it with the full engine instead. ErrUnsupported = errors.New("mist: unsupported") // ErrUndefined is a strict-mode undefined variable. The full engine fails too, though a // syntax error later in the template may take precedence there. ErrUndefined = errors.New("mist: undefined variable") )
Functions ¶
func Append ¶ added in v0.2.0
Append is Render appending to dst, so callers can reuse a buffer and render without allocating. On error out is nil.
Types ¶
type Constructs ¶ added in v0.5.0
type Constructs uint32
Constructs is a set of template constructs a Dialect can reject.
const ( TrimMarkers Constructs = 1 << iota // {{- -}} {%- -%} NegativeLiterals // -3, including negative indexes UnspacedOperators // a comparison operator with no whitespace before it: x==2 RawBlocks // {% raw %}…{% endraw %} BlankKeyword // == blank, != blank )
type Dialect ¶ added in v0.5.0
type Dialect struct {
// Output appends a printed non-string value: a number, bool, array or object.
// Strings, nil and undefined never reach it.
Output func(dst []byte, v any) ([]byte, error)
// Compare evaluates ==, !=, <, >, <=, >= in conditions.
Compare func(op string, a, b any) (bool, error)
// Reject makes these constructs bail, in both Render and Check.
Reject Constructs
// NoDefaultLeniency turns off liquidjs's rule that a leading `| default`
// makes an undefined input lenient under strict.
NoDefaultLeniency bool
}
Dialect changes how values print and compare, and which constructs mist accepts, so an application can target a Liquid engine that differs from liquidjs. With a dialect set, matching that engine is up to the dialect's author.
type Engine ¶ added in v0.3.0
type Engine struct {
// Tags maps inline tag names, as in {% name args %}, to their implementations.
// Built-in tag names can't be overridden.
Tags map[string]TagFunc
// Filters maps filter names, as in {{ x | name: arg }}, to their implementations.
// They override built-in filters of the same name, as registerFilter does in liquidjs.
Filters map[string]FilterFunc
// Dialect, if set, changes how values print and compare and which constructs
// bail. nil means liquidjs, the SPEC parity target.
Dialect *Dialect
// Now is the clock for the date filter's 'now' and 'today'. nil means time.Now.
Now func() time.Time
}
Engine renders templates with custom tags and filters. The zero value has none, and the package-level functions use it.
func (Engine) Append ¶ added in v0.3.0
func (e Engine) Append(dst []byte, tpl string, vars map[string]any, strict bool) (out []byte, err error)
Append is the package-level Append with e's custom tags.
func (Engine) Check ¶ added in v0.3.0
Check is the package-level Check, also accepting e's custom tags.
type Error ¶
type Filter ¶ added in v0.4.0
type Filter struct {
Name string
Input any // nil for both nil and undefined
Args []any // positional arguments
Strict bool
}
Filter is one application of a registered filter.
type FilterFunc ¶ added in v0.4.0
FilterFunc implements a filter. Returning an error that wraps ErrUnsupported hands the template to the full engine; any other error stops rendering.
type Keyword ¶ added in v0.5.0
type Keyword string
Keyword is how a keyword literal reaches Dialect.Compare.
const Blank Keyword = "blank"
Blank is the blank keyword as Dialect.Compare sees it.
type Result ¶
type Result struct {
Out string
Err error // ErrUndefined or a custom tag's error; unsupported steps are never returned
}
func RenderChain ¶
RenderChain renders steps in order, binding each output into the vars seen by later steps (see SPEC.md § Chains). It stops at the first step mist can't render and returns n, its index; the caller renders steps[n:] with the full engine using the returned vars. The caller's maps are never mutated.
type Step ¶
type Step struct {
Body string
Key []string // where the output is bound for later steps
Strict bool
Vars map[string]any // replaces the chain vars for this step only
}
Step is one template in a RenderChain.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
mistcheck
command
Command mistcheck reports whether Liquid templates are inside the mist subset.
|
Command mistcheck reports whether Liquid templates are inside the mist subset. |
|
scripts
|
|
|
gen
command
Command gen writes random templates drawn from the SPEC.md grammar, with random data, in harvest.rb's format for oracle.mjs.
|
Command gen writes random templates drawn from the SPEC.md grammar, with random data, in harvest.rb's format for oracle.mjs. |