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 1c4e682fbe7551798a246541dfd65a7713b7b318
parent faf54bb74b875988551ba7e70388b1bf45ff246f
Author: gearsix <gearsix@tuta.io>
Date:   Mon,  8 Mar 2021 17:28:04 +0000

added ExecuteTemplate

Diffstat:
Msrc/template.go | 25+++++++++++++++++++++++++
1 file changed, 25 insertions(+), 0 deletions(-)

diff --git a/src/template.go b/src/template.go @@ -1,8 +1,10 @@ package main import ( + "bytes" "fmt" hmpl "html/template" + "reflect" "os" "path/filepath" "strings" @@ -60,3 +62,25 @@ func LoadTemplateFile(root string, partials ...string) (t Template, e error) { return } + +func ExecuteTemplate(t Template, d Data) (result bytes.Buffer, err error) { + if t == nil || d == nil { + err = fmt.Errorf("missing parameters") + return + } + + tv := reflect.ValueOf(t) + tt := reflect.TypeOf(t) + if tt.String() == "*template.Template" { // tmpl or hmpl + rval := tv.MethodByName("Execute").Call([]reflect.Value{ + reflect.ValueOf(&result), reflect.ValueOf(&d), + }) + if rval[0].IsNil() == false { + err = rval[0].Interface().(error) + } + } else { + err = fmt.Errorf("unable to infer template type '%s'", tt.String()) + } + + return +} +\ No newline at end of file