♻️ Include template utils in theme repository

This commit is contained in:
Maxim Lebedev 2023-11-09 05:05:00 +06:00
parent a475dacd60
commit 0ad5c9694f
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
1 changed files with 12 additions and 11 deletions

View File

@ -11,23 +11,24 @@ import (
)
type fileSystemThemeRepository struct {
dir fs.FS
dir fs.FS
funcMap template.FuncMap
}
var helpers = template.FuncMap{
"safeHTML": templateutil.SafeHTML,
}
func NewFileSystemThemeRepository(dir fs.FS) theme.Repository {
return &fileSystemThemeRepository{
dir: dir,
func NewFileSystemThemeRepository(dir fs.FS) (theme.Repository, error) {
funcMap, err := templateutil.New(dir)
if err != nil {
return nil, fmt.Errorf("cannot setup template.FuncMap for theme templates: %w", err)
}
return &fileSystemThemeRepository{
dir: dir,
funcMap: funcMap,
}, nil
}
func (repo *fileSystemThemeRepository) Get(ctx context.Context) (*template.Template, error) {
tpl, err := template.New("").
Funcs(helpers).
ParseFS(repo.dir, "baseof.html", "single.html")
tpl, err := template.New("").Funcs(repo.funcMap).ParseFS(repo.dir, "*.html")
if err != nil {
return nil, fmt.Errorf("cannot find baseof template: %w", err)
}