Documentation
¶
Overview ¶
Package template evaluates `{{ }}` expressions and renders templates with typed variables.
Index ¶
- Variables
- type Env
- func (e *Env) Eval(x syntax.Expr) (value.Value, error)
- func (e *Env) File(name *syntax.Template) ([]byte, error)
- func (e *Env) Regex(n syntax.Node, span syntax.Span) (value.Regex, error)
- func (e *Env) Render(t *syntax.Template) (string, error)
- func (e *Env) RenderExpr(x syntax.Expr) (string, error)
- func (e *Env) RenderJSON(v syntax.JSONValue, keepSpace bool) (string, error)
- func (e *Env) RenderMultiline(m *syntax.MultilineString) (string, error)
- type Variable
- type Vars
Constants ¶
This section is empty.
Variables ¶
var ErrFileAccessDenied = errors.New("file access denied")
ErrFileAccessDenied reports a file outside the allowed root.
Functions ¶
This section is empty.
Types ¶
type Env ¶
type Env struct {
Vars Vars
Now func() time.Time
UUID func() string
// ReadFile reads a file named in the request file. It returns an error
// wrapping ErrFileAccessDenied for a path outside the allowed root.
ReadFile func(name string) ([]byte, error)
// Missing, when set, is consulted by Eval for a variable Vars does
// not define, instead of failing with an UndefinedVariable error: it
// returns the value to use in its place and whether it applied
// (false falls through to the normal undefined-variable error, so
// Missing may itself be selective). A run never sets this (nil,
// meaning every undefined variable is an error, as always); it
// exists for engine.RenderCurl, which renders a curl command line
// for a variable a capture would only define once the entry actually
// ran, and must not fail just because that hasn't happened. Vars
// itself is never mutated to remember a Missing result: each call
// site (an entry, in RenderCurl's case) gets its own answer, so nothing
// leaks to a different context sharing the same Env.
Missing func(name string) (value.Value, bool)
}
Env is the evaluation environment of an entry: its variables, the sources of the template functions and file access. Now and UUID default to the system clock and a random version 4 UUID; without ReadFile no file can be read.
func (*Env) Regex ¶
Regex returns the regex of a filter, query or predicate argument: a /regex/ literal, or a string template compiled after rendering. span locates errors on a literal.
func (*Env) Render ¶
Render renders a template. Text after a placeholder's expression (as in `{{a b}}`) is ignored.
func (*Env) RenderExpr ¶
RenderExpr evaluates an expression and renders its value as text.
func (*Env) RenderJSON ¶
RenderJSON renders a JSON value with its placeholders; keepSpace keeps the white space as written.
func (*Env) RenderMultiline ¶
func (e *Env) RenderMultiline(m *syntax.MultilineString) (string, error)
RenderMultiline renders a multiline string. Raw strings are not templated; a GraphQL string becomes the JSON document `{"query":…,"variables":…}`.