dati

A Go library/binary to parse & execute data against template langauges.
git clone git://src.gearsix.net/dati
Log | Files | Refs | Atom | README | LICENSE

commit 092dba3542deb26686332168d92a6f347df5b088
parent 4115fb01e84d65354a5352b8a42425dbf7f34cac
Author: gearsix <gearsix@tuta.io>
Date:   Thu, 16 Mar 2023 15:54:48 +0000

template.go: added (*Template).ExecuteToFile

Diffstat:
MCHANGELOG | 4++++
Mtemplate.go | 29+++++++++++++++++++++++++++++
2 files changed, 33 insertions(+), 0 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG @@ -1,5 +1,9 @@ # CHANGELOG +## v1.2.4 + +- added (*Template).ExecuteToFile + ## v1.2.3 - made sure the errors returned by `LoadData` and `WriteData` always indicates the DataFormat being parsed. diff --git a/template.go b/template.go @@ -135,6 +135,34 @@ func (t *Template) Execute(data interface{}) (result bytes.Buffer, err error) { return } +// ExecuteToFile writes the result of `(*Template).Execute(data)` to the file at `path` (if no errors occurred). +// If `force` is true, any existing file at `path` will be overwritten. +func (t *Template)ExecuteToFile(data interface{}, path string, force bool) (file *os.File, err error) { + if f, err := os.Open(path); os.IsNotExist(err) { + f, err = os.Create(path) + } else if !force { + err = os.ErrExist + } else { // overwrite existing file data + if err = f.Truncase(0); err == nil { + _, err = f.Seek(0, 0)i + } + } + + if err != nil { + return + } + defer f.Close() + + var out bytes.Buffer + if out, err = t.Execute(data); err != nil { + f = nil + } else { + _, err = f.Write(out.Bytes()) + } + + return +} + // LoadTemplateFilepath loads a Template from file `root`. All files in `partials` // that have the same template type (identified by file extension) are also // parsed and associated with the parsed root template. @@ -274,3 +302,4 @@ func loadTemplateMst(rootName string, root io.Reader, partials map[string]io.Rea return template, nil } +