Documentation ¶
Index ¶
- Variables
- type FSTag
- type HTML
- type NamedReader
- type Namespace
- func (n *Namespace) ExecuteTemplate(w io.Writer, name string, vars map[string]any) error
- func (n *Namespace) GetTemplate(name string) (Template, bool)
- func (n *Namespace) MustGetTemplate(name string) Template
- func (n *Namespace) Parse(r NamedReader) (Template, error)
- func (t *Namespace) ParseBytes(filename string, tmpl []byte) (Template, error)
- func (t *Namespace) ParseFS(fsys fs.FS, path string) (Template, error)
- func (t *Namespace) ParseFSGlob(fsys fs.FS, glob string) error
- func (t *Namespace) ParseFile(path string) (Template, error)
- func (t *Namespace) ParseGlob(glob string) error
- func (t *Namespace) ParseString(filename, tmpl string) (Template, error)
- func (n *Namespace) ParseWithName(name string, r io.Reader) (Template, error)
- func (n *Namespace) WithEscapeHTML(b bool) *Namespace
- func (n *Namespace) WithNilToZero(b bool) *Namespace
- func (n *Namespace) WithTagMap(m map[string]Tag) *Namespace
- func (n *Namespace) WithVarMap(m map[string]any) *Namespace
- func (n *Namespace) WithWhitespaceMutations(b bool) *Namespace
- func (n *Namespace) WithWriteOnSuccess(b bool) *Namespace
- type Tag
- type TagContext
- func (tc *TagContext) Execute(nodes []ast.Node, local map[string]any) error
- func (tc *TagContext) ExecuteToMemory(nodes []ast.Node, local map[string]any) ([]byte, error)
- func (tc *TagContext) GetValue(node ast.Node, local map[string]any) (any, error)
- func (tc *TagContext) NodeToString(node ast.Node) string
- func (tc *TagContext) PosError(node ast.Node, format string, v ...any) error
- func (tc *TagContext) Write(b []byte) (int, error)
- type Template
- func (t Template) Execute(w io.Writer) error
- func (t Template) WithEscapeHTML(b bool) Template
- func (t Template) WithNilToZero(b bool) Template
- func (t Template) WithTagMap(m map[string]Tag) Template
- func (t Template) WithVarMap(m map[string]any) Template
- func (t Template) WithWriteOnSuccess(b bool) Template
Constants ¶
This section is empty.
Variables ¶
var ( ErrIncludeInvalidArgs = errors.New("include expects one string argument") ErrNoSuchTemplate = errors.New("no such template") )
Functions ¶
This section is empty.
Types ¶
type FSTag ¶
type FSTag struct { // FS is the filesystem that files will be loaded from. FS fs.FS // PathPrefix is joined to the path string before a file is read. PathPrefix string // Extension is appended to the end of the path string before a file is read. Extension string }
FSTag writes files from an fs.FS to a template
No escaping is done on the files, so make sure to avoid user-generated data.
type NamedReader ¶
NamedReader is a reader with a name
type Namespace ¶
type Namespace struct { // WhitespaceMutations enables postprocessing to remove whitespace where it isn't needed // to make the resulting document look better. Postprocessing is only done once when the // template is parsed, so it will not affect performance. (default: true) WhitespaceMutations bool // WriteOnSuccess indicates whether the output should only be written if generation fully succeeds. // This option buffers the output of the template, so it will use more memory. (default: false) WriteOnSuccess bool // NilToZero indictes whether nil pointer values should be converted to zero values of their underlying // types. NilToZero bool // contains filtered or unexported fields }
Namespace represents a collection of templates that can include each other
func (*Namespace) ExecuteTemplate ¶
ExecuteTemplate gets and executes a template with the given name.
func (*Namespace) GetTemplate ¶
GetTemplate tries to get a template from the namespace's template map. If it finds the template, it returns the template and true. If it doesn't find it, it returns nil and false.
func (*Namespace) MustGetTemplate ¶
MustGetTemplate is the same as GetTemplate but it panics if the template doesn't exist in the namespace.
func (*Namespace) Parse ¶
func (n *Namespace) Parse(r NamedReader) (Template, error)
Parse parses a salix template from a NamedReader, which is an io.Reader with a Name method that returns a string. os.File implements NamedReader.
func (*Namespace) ParseBytes ¶
ParseString parses bytes using the given filename.
func (*Namespace) ParseFS ¶
ParseFile parses a file at the given path in a filesystem. It uses the path as the name.
func (*Namespace) ParseFSGlob ¶
ParseGlob parses all the files in the filesystem that were matched by the given glob and adds them to the namespace.
func (*Namespace) ParseFile ¶
ParseFile parses the file at path as a salix template. It uses the path as the name.
func (*Namespace) ParseGlob ¶
ParseGlob parses all the files that were matched by the given glob nd adds them to the namespace.
func (*Namespace) ParseString ¶
ParseString parses a string using the given filename.
func (*Namespace) ParseWithName ¶
ParseWithFilename parses a salix template from r, using the given name.
func (*Namespace) WithEscapeHTML ¶
WithEscapeHTML turns HTML escaping on or off for the namespace
func (*Namespace) WithNilToZero ¶
WithNilToZero enables or disables conversion of nil values to zero values for the namespace
func (*Namespace) WithTagMap ¶
WithTagMap sets the namespace's tag map to m
func (*Namespace) WithVarMap ¶
WithVarMap sets the namespace's variable map to m
func (*Namespace) WithWhitespaceMutations ¶
WithWhitespaceMutations turns whitespace mutations on or off for the namespace
func (*Namespace) WithWriteOnSuccess ¶
WithWriteOnSuccess enables or disables only writing if generation fully succeeds.
type Tag ¶
type Tag interface {
Run(tc *TagContext, block, args []ast.Node) error
}
Tag represents a tag in a Salix template
type TagContext ¶
TagContext is passed to Tag implementations to allow them to control the interpreter
func (*TagContext) Execute ¶
Execute runs the interpreter on the given AST nodes, with the given local variables.
func (*TagContext) ExecuteToMemory ¶
ExecuteToMemory runs the interpreter on the given AST nodes, with the given local variables, and returns the resulting bytes rather than writing them out.
func (*TagContext) GetValue ¶
GetValue evaluates the given AST node using the given local variables.
func (*TagContext) NodeToString ¶
func (tc *TagContext) NodeToString(node ast.Node) string
NodeToString returns a textual representation of the given AST node for users to see, such as in error messages. This does not directly correlate to Salix source code.
type Template ¶
type Template struct { // WriteOnSuccess indicates whether the output should only be written if generation fully succeeds. // This option buffers the output of the template, so it will use more memory. (default: false) WriteOnSuccess bool NilToZero bool // contains filtered or unexported fields }
Template represents a Salix template
func (Template) WithEscapeHTML ¶
WithEscapeHTML returns a copy of the template with HTML escaping enabled or disabled. The HTML escaping functionality is NOT context-aware. Using the HTML type allows you to get around the escaping if needed.
func (Template) WithNilToZero ¶
WithNilToZero enables or disables conversion of nil values to zero values.
func (Template) WithTagMap ¶
WithTagMap returns a copy of the template with its tag map set to m.
func (Template) WithVarMap ¶
WithVarMap returns a copy of the template with its variable map set to m.
func (Template) WithWriteOnSuccess ¶
WithWriteOnSuccess enables or disables only writing if generation fully succeeds.