diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index 931180b..9917f97 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -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,43 +48,31 @@ 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 { - // 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 - // 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"). - Key(string(page.Name)). - Base(string(page.Layout)). - Files( - fmt.Sprintf("layouts/%s", page.Layout), - fmt.Sprintf("pages/%s", page.Name), - ). - Directories("components"). - Execute(page) + // 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 + // 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(templateGroup). + Key(string(page.Name)). + Base(string(page.Layout)). + Files( + fmt.Sprintf("layouts/%s", page.Layout), + fmt.Sprintf("pages/%s", page.Name), + ). + Directories("components"). + Execute(page) + if err != nil { return c.Fail(err, "failed to parse and execute templates") } diff --git a/pkg/controller/controller_test.go b/pkg/controller/controller_test.go index 2334fa9..b12b7d8 100644 --- a/pkg/controller/controller_test.go +++ b/pkg/controller/controller_test.go @@ -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 diff --git a/pkg/services/template_renderer_test.go b/pkg/services/template_renderer_test.go index 3c8d8f8..e3e76ba 100644 --- a/pkg/services/template_renderer_test.go +++ b/pkg/services/template_renderer_test.go @@ -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) diff --git a/templates/htmx.gohtml b/templates/layouts/htmx.gohtml similarity index 100% rename from templates/htmx.gohtml rename to templates/layouts/htmx.gohtml diff --git a/templates/templates.go b/templates/templates.go index a116ada..bb858bb 100644 --- a/templates/templates.go +++ b/templates/templates.go @@ -17,6 +17,7 @@ type ( const ( LayoutMain Layout = "main" LayoutAuth Layout = "auth" + LayoutHTMX Layout = "htmx" ) const (