diff --git a/dynamic.go b/dynamic.go index 0642d50..ba2d74c 100644 --- a/dynamic.go +++ b/dynamic.go @@ -57,28 +57,29 @@ type templateBuilder struct { templateString string funcMap template.FuncMap templateStrings []string + options TemplateOptions } func (tb templateBuilder) buildTemplate() *template.Template { switch tb.buildType { case templateType: - return tb.tmpl + return tb.tmpl.Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter) case filesTemplateType: - return template.Must(template.ParseFiles(tb.files...)) + return template.Must(template.ParseFiles(tb.files...)).Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter) case globTemplateType: - return template.Must(template.ParseGlob(tb.glob)) + return template.Must(template.ParseGlob(tb.glob)).Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter) case fsTemplateType: - return template.Must(template.ParseFS(tb.fsys, tb.files...)) + return template.Must(template.ParseFS(tb.fsys, tb.files...)).Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter) case stringTemplateType: - return template.Must(template.New(tb.templateName).Parse(tb.templateString)) + return template.Must(template.New(tb.templateName).Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter).Parse(tb.templateString)) case stringFuncTemplateType: - tmpl := template.New(tb.templateName).Funcs(tb.funcMap) + tmpl := template.New(tb.templateName).Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter).Funcs(tb.funcMap) for _, ts := range tb.templateStrings { tmpl = template.Must(tmpl.Parse(ts)) } return tmpl case filesFuncTemplateType: - return template.Must(template.New(tb.templateName).Funcs(tb.funcMap).ParseFiles(tb.files...)) + return template.Must(template.New(tb.templateName).Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter).Funcs(tb.funcMap).ParseFiles(tb.files...)) default: panic("Invalid builder type for dynamic template") } @@ -92,14 +93,14 @@ func (r DynamicRender) Add(name string, tmpl *template.Template) { if len(name) == 0 { panic("template name cannot be empty") } - builder := &templateBuilder{templateName: name, tmpl: tmpl} + builder := &templateBuilder{templateName: name, tmpl: tmpl, options: *NewTemplateOptions()} builder.buildType = templateType r[name] = builder } // AddFromFiles supply add template from files func (r DynamicRender) AddFromFiles(name string, files ...string) *template.Template { - builder := &templateBuilder{templateName: name, files: files} + builder := &templateBuilder{templateName: name, files: files, options: *NewTemplateOptions()} builder.buildType = filesTemplateType r[name] = builder return builder.buildTemplate() @@ -107,7 +108,7 @@ func (r DynamicRender) AddFromFiles(name string, files ...string) *template.Temp // AddFromGlob supply add template from global path func (r DynamicRender) AddFromGlob(name, glob string) *template.Template { - builder := &templateBuilder{templateName: name, glob: glob} + builder := &templateBuilder{templateName: name, glob: glob, options: *NewTemplateOptions()} builder.buildType = globTemplateType r[name] = builder return builder.buildTemplate() @@ -122,7 +123,7 @@ func (r DynamicRender) AddFromFS(name string, fsys fs.FS, files ...string) *temp // AddFromString supply add template from strings func (r DynamicRender) AddFromString(name, templateString string) *template.Template { - builder := &templateBuilder{templateName: name, templateString: templateString} + builder := &templateBuilder{templateName: name, templateString: templateString, options: *NewTemplateOptions()} builder.buildType = stringTemplateType r[name] = builder return builder.buildTemplate() @@ -133,6 +134,19 @@ func (r DynamicRender) AddFromStringsFuncs(name string, funcMap template.FuncMap builder := &templateBuilder{ templateName: name, funcMap: funcMap, templateStrings: templateStrings, + options: *NewTemplateOptions(), + } + builder.buildType = stringFuncTemplateType + r[name] = builder + return builder.buildTemplate() +} + +// AddFromStringsFuncsWithOptions supply add template from strings with options +func (r DynamicRender) AddFromStringsFuncsWithOptions(name string, funcMap template.FuncMap, options TemplateOptions, templateStrings ...string) *template.Template { + builder := &templateBuilder{ + templateName: name, funcMap: funcMap, + templateStrings: templateStrings, + options: options, } builder.buildType = stringFuncTemplateType r[name] = builder @@ -142,7 +156,16 @@ func (r DynamicRender) AddFromStringsFuncs(name string, funcMap template.FuncMap // AddFromFilesFuncs supply add template from file callback func func (r DynamicRender) AddFromFilesFuncs(name string, funcMap template.FuncMap, files ...string) *template.Template { tname := filepath.Base(files[0]) - builder := &templateBuilder{templateName: tname, funcMap: funcMap, files: files} + builder := &templateBuilder{templateName: tname, funcMap: funcMap, files: files, options: *NewTemplateOptions()} + builder.buildType = filesFuncTemplateType + r[name] = builder + return builder.buildTemplate() +} + +// AddFromFilesFuncs supply add template from file callback func +func (r DynamicRender) AddFromFilesFuncsWithOptions(name string, funcMap template.FuncMap, options TemplateOptions, files ...string) *template.Template { + tname := filepath.Base(files[0]) + builder := &templateBuilder{templateName: tname, funcMap: funcMap, files: files, options: options} builder.buildType = filesFuncTemplateType r[name] = builder return builder.buildTemplate() diff --git a/multitemplate.go b/multitemplate.go index 09c2978..ef0d1a2 100644 --- a/multitemplate.go +++ b/multitemplate.go @@ -11,6 +11,49 @@ import ( // Render type type Render map[string]*template.Template +type TemplateOptions struct { + LeftDelimiter string + RightDelimiter string +} + +type TemplateOption func(*TemplateOptions) + +func WithLeftDelimiter(delim string) TemplateOption { + return func(t *TemplateOptions) { + t.LeftDelimiter = delim + } +} + +func WithRightDelimiter(delim string) TemplateOption { + return func(t *TemplateOptions) { + t.RightDelimiter = delim + } +} + +func Delims(leftDelim, rightDelim string) TemplateOption { + return func(t *TemplateOptions) { + WithLeftDelimiter(leftDelim)(t) + WithRightDelimiter(rightDelim)(t) + } +} + +func NewTemplateOptions(opts ...TemplateOption) *TemplateOptions { + const ( + defaultLeftDelim = "{{" + defaultRightDelim = "}}" + ) + + t := &TemplateOptions{ + LeftDelimiter: defaultLeftDelim, + RightDelimiter: defaultRightDelim, + } + + for _, opt := range opts { + opt(t) + } + + return t +} var ( _ render.HTMLRender = Render{} @@ -76,6 +119,18 @@ func (r Render) AddFromStringsFuncs(name string, funcMap template.FuncMap, templ return tmpl } +// AddFromStringsFuncsWithOptions supply add template from strings with options +func (r Render) AddFromStringsFuncsWithOptions(name string, funcMap template.FuncMap, options TemplateOptions, templateStrings ...string) *template.Template { + tmpl := template.New(name).Delims(options.LeftDelimiter, options.RightDelimiter).Funcs(funcMap) + + for _, ts := range templateStrings { + tmpl = template.Must(tmpl.Parse(ts)).Delims(options.LeftDelimiter, options.RightDelimiter) + } + + r.Add(name, tmpl) + return tmpl +} + // AddFromFilesFuncs supply add template from file callback func func (r Render) AddFromFilesFuncs(name string, funcMap template.FuncMap, files ...string) *template.Template { tname := filepath.Base(files[0]) @@ -84,6 +139,14 @@ func (r Render) AddFromFilesFuncs(name string, funcMap template.FuncMap, files . return tmpl } +// AddFromFilesFuncsWithOptions supply add template from file callback func with options +func (r Render) AddFromFilesFuncsWithOptions(name string, funcMap template.FuncMap, options TemplateOptions, files ...string) *template.Template { + tname := filepath.Base(files[0]) + tmpl := template.Must(template.New(tname).Delims(options.LeftDelimiter, options.RightDelimiter).Funcs(funcMap).ParseFiles(files...)) + r.Add(name, tmpl) + return tmpl +} + // Instance supply render string func (r Render) Instance(name string, data interface{}) render.Render { return render.HTML{ diff --git a/renderer.go b/renderer.go index 2e48737..ac457ea 100644 --- a/renderer.go +++ b/renderer.go @@ -19,5 +19,7 @@ type Renderer interface { AddFromFS(name string, fsys fs.FS, files ...string) *template.Template AddFromString(name, templateString string) *template.Template AddFromStringsFuncs(name string, funcMap template.FuncMap, templateStrings ...string) *template.Template + AddFromStringsFuncsWithOptions(name string, funcMap template.FuncMap, options TemplateOptions, templateStrings ...string) *template.Template AddFromFilesFuncs(name string, funcMap template.FuncMap, files ...string) *template.Template + AddFromFilesFuncsWithOptions(name string, funcMap template.FuncMap, options TemplateOptions, files ...string) *template.Template }