fix: templates now parse with the same func map as the page handler
This commit is contained in:
@@ -2,43 +2,73 @@ package templates
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"io/fs"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTemplateParsing(t *testing.T) {
|
func templateFuncMap() template.FuncMap {
|
||||||
templateDir := "./"
|
return template.FuncMap{
|
||||||
|
"formatTime": func(t time.Time) string {
|
||||||
|
if t.IsZero() {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return t.Format("02 Jan 2006 15:04")
|
||||||
|
},
|
||||||
|
"truncate": func(s string, length int) string {
|
||||||
|
if len(s) <= length {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
if length <= 3 {
|
||||||
|
return s[:length]
|
||||||
|
}
|
||||||
|
return s[:length-3] + "..."
|
||||||
|
},
|
||||||
|
"substr": func(s string, start, length int) string {
|
||||||
|
if start >= len(s) {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
end := start + length
|
||||||
|
if end > len(s) {
|
||||||
|
end = len(s)
|
||||||
|
}
|
||||||
|
return s[start:end]
|
||||||
|
},
|
||||||
|
"upper": strings.ToUpper,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var templateFiles []string
|
func TestTemplateParsing(t *testing.T) {
|
||||||
err := filepath.WalkDir(templateDir, func(path string, d fs.DirEntry, err error) error {
|
layoutPath := filepath.Join(".", "base.gohtml")
|
||||||
if err != nil {
|
require.FileExists(t, layoutPath, "base layout is required for all templates")
|
||||||
return err
|
|
||||||
}
|
partials, err := filepath.Glob(filepath.Join(".", "partials", "*.gohtml"))
|
||||||
if !d.IsDir() && filepath.Ext(path) == ".gohtml" {
|
|
||||||
templateFiles = append(templateFiles, path)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
tmpl := template.New("test")
|
pages, err := filepath.Glob(filepath.Join(".", "*.gohtml"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotEmpty(t, pages, "no page templates found")
|
||||||
|
|
||||||
tmpl = tmpl.Funcs(template.FuncMap{
|
for _, page := range pages {
|
||||||
"formatTime": func(any) string { return "2024-01-01" },
|
if filepath.Base(page) == "base.gohtml" {
|
||||||
"eq": func(a, b any) bool { return a == b },
|
continue
|
||||||
"ne": func(a, b any) bool { return a != b },
|
}
|
||||||
"len": func(s any) int { return 0 },
|
|
||||||
"range": func(s any) any { return s },
|
|
||||||
})
|
|
||||||
|
|
||||||
for _, file := range templateFiles {
|
page := page
|
||||||
t.Run(file, func(t *testing.T) {
|
t.Run(filepath.Base(page), func(t *testing.T) {
|
||||||
_, err := tmpl.ParseFiles(file)
|
t.Parallel()
|
||||||
assert.NoError(t, err, "Template %s should parse without errors", file)
|
|
||||||
|
files := append([]string{layoutPath}, partials...)
|
||||||
|
files = append(files, page)
|
||||||
|
|
||||||
|
tmpl, err := template.New(filepath.Base(page)).Funcs(templateFuncMap()).ParseFiles(files...)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, tmpl.Lookup("layout"), "layout template should be available")
|
||||||
|
require.NotNil(t, tmpl.Lookup("content"), "content block should be defined by page templates")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user