dati

data and template interface
git clone git://git.gearsix.net/dati
Log | Files | Refs | Atom | README | LICENSE

commit 99f082eae87e99348c6d22c2015ad4b91964fd7c
parent 550d80f2d8fcca03e6a736127b585a4b5f9cae81
Author: gearsix <gearsix@tuta.io>
Date:   Sat,  2 Oct 2021 16:47:36 +0100

new LoadTemplate funcs accept params to name of root & partials

Diffstat:
Mtemplate.go | 38++++++++++++++++++--------------------
Mtemplate_test.go | 82++++++++++++++++++++++++++++++++++++++++++++++---------------------------------
2 files changed, 66 insertions(+), 54 deletions(-)

diff --git a/template.go b/template.go @@ -27,7 +27,6 @@ import ( "os" "path/filepath" "reflect" - "strconv" "strings" tmpl "text/template" ) @@ -219,13 +218,16 @@ func LoadTemplateFilepath(rootPath string, partialPaths ...string) (t Template, return } -// LoadTemplateString -func LoadTemplateString(lang string, name string, root string, partials ...string) (t Template, e error) { - var p []io.Reader - for _, partial := range partials { - p = append(p, strings.NewReader(partial)) +// LoadTemplateString will convert `root` and `partials` data to io.StringReader variables and +// return a `LoadTemplate` call using them as parameters. +// The `partials` map should have the template name to assign the partial template to in the +// string key and the template data in as the value. +func LoadTemplateString(lang string, rootName string, root string, partials map[string]string) (t Template, e error) { + p := make(map[string]io.Reader) + for name, partial := range partials { + p[name] = strings.NewReader(partial) } - return LoadTemplate(lang, name, strings.NewReader(root), p...) + return LoadTemplate(lang, rootName, strings.NewReader(root), p) } // LoadTemplate loads a Template from `root` of type `lang`, named @@ -234,16 +236,12 @@ func LoadTemplateString(lang string, name string, root string, partials ...strin // `root` should be a string of template, with syntax matching that of // `lang`. `partials` should be a string of template, with syntax // matching that of `lang`. -func LoadTemplate(lang string, name string, root io.Reader, partials ...io.Reader) (t Template, e error) { +func LoadTemplate(lang string, rootName string, root io.Reader, partials map[string]io.Reader) (t Template, e error) { if IsSupportedTemplateLang(lang) == -1 { e = fmt.Errorf("invalid type '%s'", lang) return } - if len(name) == 0 { - name = "template" - } - var buf []byte switch(lang) { case "tmpl": @@ -251,14 +249,14 @@ func LoadTemplate(lang string, name string, root io.Reader, partials ...io.Reade if buf, e = io.ReadAll(root); e != nil { break } - if template, e = tmpl.New(name).Parse(string(buf)); e != nil { + if template, e = tmpl.New(rootName).Parse(string(buf)); e != nil { break } - for i, p := range partials { + for name, p := range partials { if buf, e = io.ReadAll(p); e != nil { break } - if _, e = template.New(name + "-partial" + strconv.Itoa(i)).Parse(string(buf)); e != nil { + if _, e = template.New(name).Parse(string(buf)); e != nil { break } } @@ -268,14 +266,14 @@ func LoadTemplate(lang string, name string, root io.Reader, partials ...io.Reade if buf, e = io.ReadAll(root); e != nil { break } - if template, e = hmpl.New(name).Parse(string(buf)); e != nil { + if template, e = hmpl.New(rootName).Parse(string(buf)); e != nil { break } - for i, p := range partials { + for name, p := range partials { if buf, e = io.ReadAll(p); e != nil { break } - if _, e = template.New(name + "-partial" + strconv.Itoa(i)).Parse(string(buf)); e != nil { + if _, e = template.New(name).Parse(string(buf)); e != nil { break } } @@ -284,11 +282,11 @@ func LoadTemplate(lang string, name string, root io.Reader, partials ...io.Reade var template *mst.Template mstpp := new(mst.StaticProvider) mstpp.Partials = make(map[string]string) - for p, partial := range partials { + for name, partial := range partials { if buf, e = io.ReadAll(partial); e != nil { break } - mstpp.Partials[name+"-partial"+strconv.Itoa(p)] = string(buf) + mstpp.Partials[name] = string(buf) } if e == nil { if buf, e = io.ReadAll(root); e != nil { diff --git a/template_test.go b/template_test.go @@ -41,7 +41,7 @@ const mstRootGood = "{{eg}} {{> mstPartialGood}}" const mstPartialGood = "{{#data}}{{eg}}{{/data}}" const mstResult = "0 00" const mstRootBad = "{{> badPartial.mst}}{{#doesnt-exist}}{{/exit}}" -const mstPartialBad = "p{{$}{{ > noexist}}" +const mstPartialBad = "p{{$}}{{ > noexist}" func TestIsSupportedTemplateLang(t *testing.T) { exts := []string{ @@ -195,45 +195,59 @@ func TestLoadTemplateFilepath(t *testing.T) { } func TestLoadTemplateString(t *testing.T) { - var gr, gp, br, bp []string - - gr = append(gr, tmplRootGood) - gp = append(gp, tmplPartialGood) - br = append(br, tmplRootBad) - bp = append(bp, tmplPartialBad) + var err error + var template Template + var templateType string + + testInvalid := func(templateType string, template Template) { + t.Logf("invalid '%s' template managed to load", templateType) + if buf, err := template.Execute(""); err == nil { + t.Fatalf("invalid '%s' template managed to execute: %s", templateType, buf.String()) + } + } - gr = append(gr, hmplRootGood) - gp = append(gp, hmplPartialGood) - br = append(br, hmplRootBad) - bp = append(bp, hmplPartialBad) + name := "test" + templateType = "tmpl" + if template, err = LoadTemplateString(templateType, name, tmplRootGood, + map[string]string{"tmplPartialGood.tmpl": tmplPartialGood}); err != nil { + t.Fatalf("'%s' template failed to load", templateType) + } + if template, err = LoadTemplateString(templateType, name, tmplRootBad, + map[string]string{"tmplPartialGood.tmpl": tmplPartialGood}); err == nil { + testInvalid(templateType, template) + } + if template, err = LoadTemplateString(templateType, name, tmplRootGood, + map[string]string{"tmplPartialGood.tmpl": tmplPartialBad}); err == nil { + testInvalid(templateType, template) + } - gr = append(gr, mstRootGood) - gp = append(gp, mstPartialGood) - br = append(br, mstRootBad) - bp = append(bp, mstPartialBad) + templateType = "hmpl" + if template, err = LoadTemplateString(templateType, name, hmplRootGood, + map[string]string{"hmplPartialGood.hmpl": hmplPartialGood}); err != nil { + t.Fatalf("'%s' template failed to load", templateType) + } + if template, err = LoadTemplateString(templateType, name, hmplRootBad, + map[string]string{"hmplPartialGood.hmpl": hmplPartialGood}); err == nil { + testInvalid(templateType, template) + } + if template, err = LoadTemplateString(templateType, name, hmplRootGood, + map[string]string{"hmplPartialGood.hmpl": hmplPartialBad}); err == nil { + testInvalid(templateType, template) + } - name := "test" - var ttype string - for g, root := range gr { // good root, good partials - ttype = SupportedTemplateLangs[g] - if template, e := LoadTemplateString(ttype, name, root, gp[g]); e != nil { - t.Fatalf("'%s' template failed to load: %s", ttype, e) - } else { - validateTemplate(t, template, ttype, name, name + "-partial0") + templateType = "mst" + if template, err = LoadTemplateString("mst", "test", mstRootGood, + map[string]string{"mstPartialGood": mstPartialGood}); err != nil { + t.Fatalf("'%s' template failed to load", templateType) } - } - for b, root := range br { // bad root, good partials - ttype = SupportedTemplateLangs[b] - if _, e := LoadTemplateString(ttype, name, root, gp...); e == nil { - t.Fatalf("no error for bad template with good partials\n") + if template, err = LoadTemplateString(templateType, name, mstRootBad, + map[string]string{"mstPartialGood": mstPartialGood}); err == nil { + testInvalid(templateType, template) } - } - for b, root := range br { // bad root, bad partials - ttype = SupportedTemplateLangs[b] - if _, e := LoadTemplateString(ttype, name, root, bp...); e == nil { - t.Fatalf("no error for bad template with bad partials\n") + if template, err = LoadTemplateString(templateType, name, mstRootGood, + map[string]string{"mstPartialGood": mstPartialBad}); err == nil { + testInvalid(templateType, template) } - } } // func TestLoadTemplateString(t *testing.T) {} // This is tested by TestLoadTemplateFilepath and TestLoadTemplateString