Add WriteOnSuccess option
This commit is contained in:
parent
1634724260
commit
745b6c0e5f
12
namespace.go
12
namespace.go
@ -17,6 +17,9 @@ type Namespace struct {
|
||||
// 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
|
||||
escapeHTML *bool
|
||||
}
|
||||
|
||||
@ -27,6 +30,7 @@ func New() *Namespace {
|
||||
vars: map[string]any{},
|
||||
tags: map[string]Tag{},
|
||||
WhitespaceMutations: true,
|
||||
WriteOnSuccess: false,
|
||||
}
|
||||
}
|
||||
|
||||
@ -66,6 +70,14 @@ func (n *Namespace) WithEscapeHTML(b bool) *Namespace {
|
||||
return n
|
||||
}
|
||||
|
||||
// WithWriteOnSuccess enables or disables only writing if generation fully succeeds.
|
||||
func (n *Namespace) WithWriteOnSuccess(b bool) *Namespace {
|
||||
n.mu.Lock()
|
||||
defer n.mu.Unlock()
|
||||
n.WriteOnSuccess = true
|
||||
return n
|
||||
}
|
||||
|
||||
// WithWhitespaceMutations turns whitespace mutations on or off for the namespace
|
||||
func (n *Namespace) WithWhitespaceMutations(b bool) *Namespace {
|
||||
n.mu.Lock()
|
||||
|
1
parse.go
1
parse.go
@ -37,6 +37,7 @@ func (n *Namespace) ParseWithName(name string, r io.Reader) (Template, error) {
|
||||
ast: astVal.([]ast.Node),
|
||||
tags: map[string]Tag{},
|
||||
vars: map[string]any{},
|
||||
WriteOnSuccess: n.WriteOnSuccess,
|
||||
}
|
||||
|
||||
if n.WhitespaceMutations {
|
||||
|
20
salix.go
20
salix.go
@ -2,6 +2,7 @@ package salix
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html"
|
||||
@ -22,6 +23,9 @@ type Template struct {
|
||||
ast []ast.Node
|
||||
|
||||
escapeHTML *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
|
||||
|
||||
tags map[string]Tag
|
||||
vars map[string]any
|
||||
@ -56,13 +60,29 @@ func (t Template) WithEscapeHTML(b bool) Template {
|
||||
return t
|
||||
}
|
||||
|
||||
// WithWriteOnSuccess enables or disables only writing if generation fully succeeds.
|
||||
func (t Template) WithWriteOnSuccess(b bool) Template {
|
||||
t.WriteOnSuccess = true
|
||||
return t
|
||||
}
|
||||
|
||||
// Execute executes a parsed template and writes
|
||||
// the result to w.
|
||||
func (t Template) Execute(w io.Writer) error {
|
||||
t.macros = map[string][]ast.Node{}
|
||||
if t.WriteOnSuccess {
|
||||
buf := &bytes.Buffer{}
|
||||
err := t.execute(buf, t.ast, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = io.Copy(w, buf)
|
||||
return err
|
||||
} else {
|
||||
bw := bufio.NewWriterSize(w, 16384)
|
||||
defer bw.Flush()
|
||||
return t.execute(bw, t.ast, nil)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Template) execute(w io.Writer, nodes []ast.Node, local map[string]any) error {
|
||||
|
Loading…
Reference in New Issue
Block a user