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 48e975183de8afbe9e38d825bc21a07c65f6d122
parent cd53c14313b6e89c5cbfb80c67434fbadfaf1651
Author: gearsix <gearsix@tuta.io>
Date:   Thu, 30 Sep 2021 00:21:03 +0100

added LoadTemplateStringTmpl

Diffstat:
Mtemplate.go | 27+++++++++++++++++++++++++--
1 file changed, 25 insertions(+), 2 deletions(-)

diff --git a/template.go b/template.go @@ -26,6 +26,7 @@ import ( "os" "path/filepath" "reflect" + "strconv" "strings" tmpl "text/template" ) @@ -110,7 +111,7 @@ func loadTemplateFileTmpl(root string, partials ...string) (*tmpl.Template, erro return err }) } else { - return nil, fmt.Errorf("non-matching filetype") + return nil, fmt.Errorf("non-matching filetype (%s)", p) } } } @@ -143,7 +144,7 @@ func loadTemplateFileHmpl(root string, partials ...string) (*hmpl.Template, erro return err }) } else { - return nil, fmt.Errorf("non-matching filetype") + return nil, fmt.Errorf("non-matching filetype (%s)", p) } } } @@ -216,3 +217,25 @@ func LoadTemplateFile(root string, partials ...string) (t Template, e error) { return } +// LoadTemplateStringTmpl +func LoadTemplateStringTmpl(name string, root string, partials ...string) (t Template, e error) { + if len(root) == 0 { + e = fmt.Errorf("no root template specified") + } + if len(name) == 0 { + name = "template" + } + + if e == nil { + template := tmpl.Must(tmpl.New(name).Parse(root)) + for i, p := range partials { + if template, e = template.New(name + "-partial" + strconv.Itoa(i)).Parse(p); e != nil { + break + } + } + t.Template = template + } + + return +} +