commit 1cc6031d4c60c13599eae881a296bae1a182f88d
parent de35484c3d9e813a4e8107a0971cc693e805b09f
Author: gearsix <gearsix@tuta.io>
Date: Tue, 30 Mar 2021 12:16:17 +0100
directories passed in the partials arg now get parsed recursively
Diffstat:
3 files changed, 22 insertions(+), 8 deletions(-)
diff --git a/data_test.go b/data_test.go
@@ -30,9 +30,9 @@ import (
var good = map[string]string{
"json": `{"eg":0}`,
"yaml": `eg: 0
- `,
+`,
"toml": `eg = 0
- `,
+`,
}
const badData = `{"json"!:2:]}}`
diff --git a/template.go b/template.go
@@ -22,6 +22,7 @@ import (
"fmt"
mst "github.com/cbroglie/mustache"
hmpl "html/template"
+ "io/fs"
"os"
"path/filepath"
"reflect"
@@ -51,8 +52,15 @@ func loadTemplateFileTmpl(root string, partials ...string) (*tmpl.Template, erro
} else if strings.Contains(p, "*") {
t, e = t.ParseGlob(p)
} else if stat.IsDir() {
- t, e = t.ParseGlob(p + "/*.tmpl")
- t, e = t.ParseGlob(p + "/*.gotmpl")
+ e = filepath.Walk(p, func(path string, info fs.FileInfo, err error) error {
+ if err == nil {
+ ptype = getTemplateType(path)
+ if ptype == "tmpl" || ptype == "gotmpl" {
+ t, err = t.ParseFiles(path)
+ }
+ }
+ return err
+ })
} else {
return nil, fmt.Errorf("non-matching filetype")
}
@@ -77,8 +85,15 @@ func loadTemplateFileHmpl(root string, partials ...string) (*hmpl.Template, erro
} else if strings.Contains(p, "*") {
t, e = t.ParseGlob(p)
} else if stat.IsDir() {
- t, e = t.ParseGlob(p + "/*.hmpl")
- t, e = t.ParseGlob(p + "/*.gohmpl")
+ e = filepath.Walk(p, func(path string, info fs.FileInfo, err error) error {
+ if err == nil {
+ ptype = getTemplateType(path)
+ if ptype == "hmpl" || ptype == "gohmpl" {
+ t, err = t.ParseFiles(path)
+ }
+ }
+ return err
+ })
} else {
return nil, fmt.Errorf("non-matching filetype")
}
diff --git a/template_test.go b/template_test.go
@@ -135,8 +135,7 @@ func TestLoadTemplateFile(t *testing.T) {
func validateExecuteTemplate(t *testing.T, results string, expect string, e error) {
if e != nil {
t.Error(e)
- }
- if results != expect {
+ } else if results != expect {
t.Errorf("invalid results: '%s' should match '%s'", results, expect)
}
}