Simplified template renderer parsing and execution.

This commit is contained in:
mikestefanello 2022-01-19 09:14:18 -05:00
parent e8d73421aa
commit 22cece2d01
8 changed files with 323 additions and 221 deletions

View file

@ -52,17 +52,17 @@ func (c *Controller) RenderPage(ctx echo.Context, page Page) error {
// 2. The content template specified in Page.Name
// 3. All templates within the components directory
// Also included is the function map provided by the funcmap package
buf, err = c.Container.TemplateRenderer.ParseAndExecute(
"page:htmx",
page.Name,
"htmx",
[]string{
buf, err = c.Container.TemplateRenderer.
Parse().
Group("page:htmx").
Key(page.Name).
Base("htmx").
Files(
"htmx",
fmt.Sprintf("pages/%s", page.Name),
},
[]string{"components"},
page,
)
).
Directories("components").
Execute(page)
} else {
// Parse and execute the templates for the Page
// As mentioned in the documentation for the Page struct, the templates used for the page will be:
@ -70,17 +70,17 @@ func (c *Controller) RenderPage(ctx echo.Context, page Page) error {
// 2. The content template specified in Page.Name
// 3. All templates within the components directory
// Also included is the function map provided by the funcmap package
buf, err = c.Container.TemplateRenderer.ParseAndExecute(
"page",
page.Name,
page.Layout,
[]string{
buf, err = c.Container.TemplateRenderer.
Parse().
Group("page").
Key(page.Name).
Base(page.Layout).
Files(
fmt.Sprintf("layouts/%s", page.Layout),
fmt.Sprintf("pages/%s", page.Name),
},
[]string{"components"},
page,
)
).
Directories("components").
Execute(page)
}
if err != nil {

View file

@ -100,7 +100,7 @@ func TestController_RenderPage(t *testing.T) {
expectedTemplates[f.Name()] = true
}
for _, v := range parsed.Templates() {
for _, v := range parsed.Template.Templates() {
delete(expectedTemplates, v.Name())
}
assert.Empty(t, expectedTemplates)
@ -133,7 +133,7 @@ func TestController_RenderPage(t *testing.T) {
expectedTemplates[f.Name()] = true
}
for _, v := range parsed.Templates() {
for _, v := range parsed.Template.Templates() {
delete(expectedTemplates, v.Name())
}
assert.Empty(t, expectedTemplates)
@ -167,7 +167,7 @@ func TestController_RenderPage(t *testing.T) {
err = c.Cache.
Flush().
Tags(p.Cache.Tags[0]).
Exec(context.Background())
Execute(context.Background())
require.NoError(t, err)
// Refetch from the cache and expect no results