Move htmx template into layouts.

This commit is contained in:
mikestefanello 2023-12-18 20:59:40 -05:00
parent c2b6928fb4
commit 97cd9d0fee
5 changed files with 29 additions and 38 deletions

View File

@ -9,6 +9,7 @@ import (
"github.com/mikestefanello/pagoda/pkg/htmx"
"github.com/mikestefanello/pagoda/pkg/middleware"
"github.com/mikestefanello/pagoda/pkg/services"
"github.com/mikestefanello/pagoda/templates"
"github.com/labstack/echo/v4"
)
@ -32,6 +33,7 @@ func NewController(c *services.Container) Controller {
func (c *Controller) RenderPage(ctx echo.Context, page Page) error {
var buf *bytes.Buffer
var err error
templateGroup := "page"
// Page name is required
if page.Name == "" {
@ -46,24 +48,13 @@ func (c *Controller) RenderPage(ctx echo.Context, page Page) error {
// Check if this is an HTMX non-boosted request which indicates that only partial
// content should be rendered
if page.HTMX.Request.Enabled && !page.HTMX.Request.Boosted {
// Parse and execute the templates only for the content portion of the page
// The templates used for this partial request will be:
// 1. The base htmx template which omits the layout and only includes the content template
// 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.
Parse().
Group("page:htmx").
Key(string(page.Name)).
Base("htmx").
Files(
"htmx",
fmt.Sprintf("pages/%s", page.Name),
).
Directories("components").
Execute(page)
} else {
// Switch the layout which will only render the page content
page.Layout = templates.LayoutHTMX
// Alter the template group so this is cached separately
templateGroup = "page:htmx"
}
// 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:
// 1. The layout/base template specified in Page.Layout
@ -72,7 +63,7 @@ func (c *Controller) RenderPage(ctx echo.Context, page Page) error {
// Also included is the function map provided by the funcmap package
buf, err = c.Container.TemplateRenderer.
Parse().
Group("page").
Group(templateGroup).
Key(string(page.Name)).
Base(string(page.Layout)).
Files(
@ -81,7 +72,6 @@ func (c *Controller) RenderPage(ctx echo.Context, page Page) error {
).
Directories("components").
Execute(page)
}
if err != nil {
return c.Fail(err, "failed to parse and execute templates")

View File

@ -93,7 +93,7 @@ func TestController_RenderPage(t *testing.T) {
// Check the template cache
parsed, err := c.TemplateRenderer.Load("page", string(p.Name))
assert.NoError(t, err)
require.NoError(t, err)
// Check that all expected templates were parsed.
// This includes the name, layout and all components
@ -126,7 +126,7 @@ func TestController_RenderPage(t *testing.T) {
// Check the template cache
parsed, err := c.TemplateRenderer.Load("page:htmx", string(p.Name))
assert.NoError(t, err)
require.NoError(t, err)
// Check that all expected templates were parsed.
// This includes the name, htmx and all components

View File

@ -24,7 +24,7 @@ func TestTemplateRenderer(t *testing.T) {
Group(group).
Key(id).
Base("htmx").
Files("htmx", "pages/error").
Files("layouts/htmx", "pages/error").
Directories("components").
Store()
require.NoError(t, err)

View File

@ -17,6 +17,7 @@ type (
const (
LayoutMain Layout = "main"
LayoutAuth Layout = "auth"
LayoutHTMX Layout = "htmx"
)
const (