Skip to content

Commit

Permalink
Add templates.WithFileFindFunc option
Browse files Browse the repository at this point in the history
  • Loading branch information
janos committed Aug 4, 2017
1 parent 0619e9b commit df9e8d5
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ var ErrUnknownTemplate = fmt.Errorf("unknown template")

// Options holds parameters for creating Templates.
type Options struct {
baseDir string
contentType string
files map[string][]string
strings map[string][]string
functions template.FuncMap
delimOpen string
delimClose string
logf func(format string, a ...interface{})
fileFindFunc func(filename string) string
contentType string
files map[string][]string
strings map[string][]string
functions template.FuncMap
delimOpen string
delimClose string
logf func(format string, a ...interface{})
}

// Option sets parameters used in New function.
Expand All @@ -57,7 +57,18 @@ func WithContentType(contentType string) Option {
// WithBaseDir sets the directory in which template files
// are stored.
func WithBaseDir(dir string) Option {
return func(o *Options) { o.baseDir = dir }
return func(o *Options) {
o.fileFindFunc = func(f string) string {
return filepath.Join(dir, f)
}
}
}

// WithFileFindFunc sets the function that will return the
// file path on disk based on filename provided from files
// defind using WithTemplateFromFile or WithTemplateFromFiles.
func WithFileFindFunc(fn func(filename string) string) Option {
return func(o *Options) { o.fileFindFunc = fn }
}

// WithTemplateFromFiles adds a template parsed from files.
Expand Down Expand Up @@ -157,7 +168,7 @@ func New(opts ...Option) (t *Templates, err error) {
for name, files := range o.files {
fs := []string{}
for _, f := range files {
fs = append(fs, filepath.Join(o.baseDir, f))
fs = append(fs, o.fileFindFunc(f))
}
tpl, err := parseFiles(template.New("").Funcs(o.functions).Delims(o.delimOpen, o.delimClose), fs...)
if err != nil {
Expand Down

0 comments on commit df9e8d5

Please sign in to comment.