Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support template delimiters #47

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 deletions dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -92,22 +93,22 @@ 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()
}

// 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()
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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()
Expand Down
63 changes: 63 additions & 0 deletions multitemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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])
Expand All @@ -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{
Expand Down
2 changes: 2 additions & 0 deletions renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}