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

feat: courier templates fs support #2164

Merged
merged 6 commits into from
Jan 20, 2022
Merged
Changes from 1 commit
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
50 changes: 46 additions & 4 deletions courier/template/load_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,30 @@ func loadBuiltInTemplate(osdir, name string, html bool) (Template, error) {
return tpl, nil
}

func loadTemplateFs(fs embed.FS, name, pattern string, html bool) (Template, error) {
if t, found := cache.Get(name); found {
return t.(Template), nil
}

var tpl Template
if html {
t, err := htemplate.New(filepath.Base(name)).Funcs(sprig.HtmlFuncMap()).ParseFS(fs, pattern)
if err != nil {
return nil, errors.WithStack(err)
}
tpl = t
} else {
t, err := template.New(filepath.Base(name)).Funcs(sprig.TxtFuncMap()).ParseFS(fs, pattern)
if err != nil {
return nil, errors.WithStack(err)
}
tpl = t
}

_ = cache.Add(name, tpl)
return tpl, nil
}

func loadTemplate(osdir, name, pattern string, html bool) (Template, error) {
if t, found := cache.Get(name); found {
return t.(Template), nil
Expand Down Expand Up @@ -105,23 +129,41 @@ func loadTemplate(osdir, name, pattern string, html bool) (Template, error) {
return tpl, nil
}

func LoadTextTemplate(osdir, name, pattern string, model interface{}) (string, error) {
t, err := loadTemplate(osdir, name, pattern, false)
func LoadTextTemplate(osdir, name, pattern string, model interface{}, fs ...embed.FS) (string, error) {
var t Template
var err error

if len(fs) != 0 && osdir == "" {
t, err = loadTemplateFs(fs[0], name, pattern, false)
} else {
t, err = loadTemplate(osdir, name, pattern, false)
}

if err != nil {
return "", err
}

var b bytes.Buffer
if err := t.Execute(&b, model); err != nil {
return "", err
}
return b.String(), nil
}

func LoadHTMLTemplate(osdir, name, pattern string, model interface{}) (string, error) {
t, err := loadTemplate(osdir, name, pattern, true)
func LoadHTMLTemplate(osdir, name, pattern string, model interface{}, fs ...embed.FS) (string, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we will only use the first embedFS argument. So maybe just make this a regular arg?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but then it won't be optional anymore. we will then require the current functions to pass a nil i guess

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Three options:

  1. nil is acceptable (okayish)
  2. just pass the FS in the caller (preferable)
  3. if more options are anticipated add an options struct (least preferable because extra work that can easily be avoided, not many options planned in the future)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, okay, I will implement option 2.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @Benehiko :)

var t Template
var err error

if len(fs) != 0 && osdir == "" {
t, err = loadTemplateFs(fs[0], name, pattern, true)
} else {
t, err = loadTemplate(osdir, name, pattern, true)
}

if err != nil {
return "", err
}

var b bytes.Buffer
if err := t.Execute(&b, model); err != nil {
return "", err
Expand Down