Skip to content

Commit

Permalink
Fixed gin-contrib#62, Added AddFromFSFuncs
Browse files Browse the repository at this point in the history
  • Loading branch information
YamiOdymel committed Aug 19, 2024
1 parent bcfbd48 commit 0872d15
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
13 changes: 13 additions & 0 deletions dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
filesTemplateType
globTemplateType
fsTemplateType
fsFuncTemplateType
stringTemplateType
stringFuncTemplateType
filesFuncTemplateType
Expand Down Expand Up @@ -69,6 +70,8 @@ func (tb templateBuilder) buildTemplate() *template.Template {
return template.Must(template.ParseGlob(tb.glob))
case fsTemplateType:
return template.Must(template.ParseFS(tb.fsys, tb.files...))
case fsFuncTemplateType:
return template.Must(template.New(tb.templateName).Funcs(tb.funcMap).ParseFS(tb.fsys, tb.files...))
case stringTemplateType:
return template.Must(template.New(tb.templateName).Parse(tb.templateString))
case stringFuncTemplateType:
Expand Down Expand Up @@ -113,13 +116,23 @@ func (r DynamicRender) AddFromGlob(name, glob string) *template.Template {
return builder.buildTemplate()
}

// AddFromFS supply add template from fs.FS (e.g. embed.FS)
func (r DynamicRender) AddFromFS(name string, fsys fs.FS, files ...string) *template.Template {
builder := &templateBuilder{templateName: name, fsys: fsys, files: files}
builder.buildType = fsTemplateType
r[name] = builder
return builder.buildTemplate()
}

// AddFromFSFuncs supply add template from fs.FS (e.g. embed.FS) with callback func
func (r DynamicRender) AddFromFSFuncs(name string, funcMap template.FuncMap, fsys fs.FS, files ...string) *template.Template {
tname := filepath.Base(files[0])
builder := &templateBuilder{templateName: tname, funcMap: funcMap, fsys: fsys, files: files}
builder.buildType = fsFuncTemplateType
r[name] = builder
return builder.buildTemplate()
}

// AddFromString supply add template from strings
func (r DynamicRender) AddFromString(name, templateString string) *template.Template {
builder := &templateBuilder{templateName: name, templateString: templateString}
Expand Down
7 changes: 7 additions & 0 deletions multitemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ func (r Render) AddFromFS(name string, fsys fs.FS, files ...string) *template.Te
return tmpl
}

// AddFromFSFuncs supply add template from fs.FS (e.g. embed.FS) with callback func
func (r Render) AddFromFSFuncs(name string, funcMap template.FuncMap, fsys fs.FS, files ...string) *template.Template {
tmpl := template.Must(template.New(name).ParseFS(fsys, files...))
r.Add(name, tmpl)
return tmpl
}

// AddFromString supply add template from strings
func (r Render) AddFromString(name, templateString string) *template.Template {
tmpl := template.Must(template.New(name).Parse(templateString))
Expand Down
1 change: 1 addition & 0 deletions renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Renderer interface {
AddFromFiles(name string, files ...string) *template.Template
AddFromGlob(name, glob string) *template.Template
AddFromFS(name string, fsys fs.FS, files ...string) *template.Template
AddFromFSFuncs(name string, funcMap template.FuncMap, fsys fs.FS, files ...string) *template.Template
AddFromString(name, templateString string) *template.Template
AddFromStringsFuncs(name string, funcMap template.FuncMap, templateStrings ...string) *template.Template
AddFromFilesFuncs(name string, funcMap template.FuncMap, files ...string) *template.Template
Expand Down

0 comments on commit 0872d15

Please sign in to comment.