Add ExecuteToMemory function to TagContext
This commit is contained in:
parent
a942ceae20
commit
9a0a20da66
18
tags.go
18
tags.go
@ -19,6 +19,7 @@
|
|||||||
package salix
|
package salix
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"go.elara.ws/salix/ast"
|
"go.elara.ws/salix/ast"
|
||||||
@ -48,11 +49,28 @@ func (tc *TagContext) Execute(nodes []ast.Node, local map[string]any) error {
|
|||||||
return tc.t.execute(tc.w, nodes, mergeMap(tc.local, local))
|
return tc.t.execute(tc.w, nodes, mergeMap(tc.local, local))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 (tc *TagContext) ExecuteToMemory(nodes []ast.Node, local map[string]any) ([]byte, error) {
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
err := tc.t.execute(buf, nodes, mergeMap(tc.local, local))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return buf.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetValue evaluates the given AST node using the given local variables.
|
// GetValue evaluates the given AST node using the given local variables.
|
||||||
func (tc *TagContext) GetValue(node ast.Node, local map[string]any) (any, error) {
|
func (tc *TagContext) GetValue(node ast.Node, local map[string]any) (any, error) {
|
||||||
return tc.t.getValue(node, mergeMap(tc.local, local))
|
return tc.t.getValue(node, mergeMap(tc.local, local))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Write writes b to the underlying writer. It implements
|
||||||
|
// the io.Writer interface.
|
||||||
|
func (tc *TagContext) Write(b []byte) (int, error) {
|
||||||
|
return tc.w.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
func mergeMap(a, b map[string]any) map[string]any {
|
func mergeMap(a, b map[string]any) map[string]any {
|
||||||
out := map[string]any{}
|
out := map[string]any{}
|
||||||
if a != nil {
|
if a != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user