commit 04039e54de0d71c309105b7d09c799ea40372ad5
parent 59550a7975485faad840fb4360a3015b39f3523b
Author: gearsix <gearsix@tuta.io>
Date: Sun, 11 Apr 2021 00:22:39 +0100
made Template a struct
- Template is now accessed via (t *Template).Template interface{}
- func ExecuteTemplate(... -> func (t *Template) ExecuteTemplate(...
Diffstat:
2 files changed, 32 insertions(+), 26 deletions(-)
diff --git a/template.go b/template.go
@@ -31,7 +31,9 @@ import (
)
// Template is a generic interface container for any template type
-type Template interface{}
+type Template struct {
+ Template interface{}
+}
func getTemplateType(path string) string {
return strings.TrimPrefix(filepath.Ext(path), ".")
@@ -142,33 +144,37 @@ func loadTemplateFileMst(root string, partials ...string) (*mst.Template, error)
// parsed and associated with the parsed root template.
func LoadTemplateFile(root string, partials ...string) (t Template, e error) {
if len(root) == 0 {
- return nil, fmt.Errorf("no root template specified")
+ e = fmt.Errorf("no root template specified")
}
if stat, err := os.Stat(root); err != nil {
- return nil, err
+ e = err
} else if stat.IsDir() {
- return nil, fmt.Errorf("root path must be a file, not a directory: %s", root)
+ e = fmt.Errorf("root path must be a file, not a directory: %s", root)
}
- ttype := getTemplateType(root)
- if ttype == "tmpl" || ttype == "gotmpl" {
- t, e = loadTemplateFileTmpl(root, partials...)
- } else if ttype == "hmpl" || ttype == "gohmpl" {
- t, e = loadTemplateFileHmpl(root, partials...)
- } else if ttype == "mst" || ttype == "mustache" {
- t, e = loadTemplateFileMst(root, partials...)
- } else {
- e = fmt.Errorf("'%s' is not a supported template language", ttype)
+ if e == nil {
+ t = Template{}
+ ttype := getTemplateType(root)
+ if ttype == "tmpl" || ttype == "gotmpl" {
+ t.Template, e = loadTemplateFileTmpl(root, partials...)
+ } else if ttype == "hmpl" || ttype == "gohmpl" {
+ t.Template, e = loadTemplateFileHmpl(root, partials...)
+ } else if ttype == "mst" || ttype == "mustache" {
+ t.Template, e = loadTemplateFileMst(root, partials...)
+ } else {
+ e = fmt.Errorf("'%s' is not a supported template language", ttype)
+ }
}
+
return
}
// ExecuteTemplate executes `t` against `d`. Reflection is used to determine
// the template type and call it's execution fuction.
-func ExecuteTemplate(t Template, d Data) (result bytes.Buffer, err error) {
- tv := reflect.ValueOf(t)
- tt := reflect.TypeOf(t)
+func (t *Template) ExecuteTemplate(d Data) (result bytes.Buffer, err error) {
+ tv := reflect.ValueOf(t.Template)
+ tt := reflect.TypeOf(t.Template)
var rval []reflect.Value
if tt.String() == "*template.Template" { // tmpl or hmpl
diff --git a/template_test.go b/template_test.go
@@ -54,7 +54,7 @@ func validateTemplateFile(t *testing.T, template Template, root string, partials
}
ttype := getTemplateType(root)
- if reflect.TypeOf(template).String() != types[ttype] {
+ if reflect.TypeOf(template.Template).String() != types[ttype] {
t.Error("invalid template loaded")
}
@@ -62,16 +62,16 @@ func validateTemplateFile(t *testing.T, template Template, root string, partials
var rv []reflect.Value
for _, p := range partials {
p = filepath.Base(p)
- rv := reflect.ValueOf(template).MethodByName("Lookup").Call([]reflect.Value{
+ rv := reflect.ValueOf(template.Template).MethodByName("Lookup").Call([]reflect.Value{
reflect.ValueOf(p),
})
if rv[0].IsNil() {
t.Errorf("missing defined template '%s'", p)
- rv = reflect.ValueOf(template).MethodByName("DefinedTemplates").Call([]reflect.Value{})
+ rv = reflect.ValueOf(template.Template).MethodByName("DefinedTemplates").Call([]reflect.Value{})
t.Log(rv)
}
}
- rv = reflect.ValueOf(template).MethodByName("Name").Call([]reflect.Value{})
+ rv = reflect.ValueOf(template.Template).MethodByName("Name").Call([]reflect.Value{})
if rv[0].String() != filepath.Base(root) {
t.Errorf("invalid template name: %s does not match %s",
rv[0].String(), filepath.Base(root))
@@ -197,18 +197,18 @@ func TestExecuteTemplate(t *testing.T) {
t.Skip("setup failure:", e)
}
- results, e = ExecuteTemplate(tmpl1, sd)
+ results, e = tmpl1.ExecuteTemplate(sd)
validateExecuteTemplate(t, results.String(), tmplResult, e)
- results, e = ExecuteTemplate(tmpl2, sd)
+ results, e = tmpl2.ExecuteTemplate(sd)
validateExecuteTemplate(t, results.String(), tmplResult, e)
- results, e = ExecuteTemplate(hmpl1, sd)
+ results, e = hmpl1.ExecuteTemplate(sd)
validateExecuteTemplate(t, results.String(), hmplResult, e)
- results, e = ExecuteTemplate(hmpl2, sd)
+ results, e = hmpl2.ExecuteTemplate(sd)
validateExecuteTemplate(t, results.String(), tmplResult, e)
- results, e = ExecuteTemplate(mst1, sd)
+ results, e = mst1.ExecuteTemplate(sd)
validateExecuteTemplate(t, results.String(), mstResult, e)
- results, e = ExecuteTemplate(mst2, sd)
+ results, e = mst2.ExecuteTemplate(sd)
validateExecuteTemplate(t, results.String(), mstResult, e)
}