Move htmx template into layouts.
This commit is contained in:
parent
c2b6928fb4
commit
97cd9d0fee
@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/mikestefanello/pagoda/pkg/htmx"
|
"github.com/mikestefanello/pagoda/pkg/htmx"
|
||||||
"github.com/mikestefanello/pagoda/pkg/middleware"
|
"github.com/mikestefanello/pagoda/pkg/middleware"
|
||||||
"github.com/mikestefanello/pagoda/pkg/services"
|
"github.com/mikestefanello/pagoda/pkg/services"
|
||||||
|
"github.com/mikestefanello/pagoda/templates"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"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 {
|
func (c *Controller) RenderPage(ctx echo.Context, page Page) error {
|
||||||
var buf *bytes.Buffer
|
var buf *bytes.Buffer
|
||||||
var err error
|
var err error
|
||||||
|
templateGroup := "page"
|
||||||
|
|
||||||
// Page name is required
|
// Page name is required
|
||||||
if page.Name == "" {
|
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
|
// Check if this is an HTMX non-boosted request which indicates that only partial
|
||||||
// content should be rendered
|
// content should be rendered
|
||||||
if page.HTMX.Request.Enabled && !page.HTMX.Request.Boosted {
|
if page.HTMX.Request.Enabled && !page.HTMX.Request.Boosted {
|
||||||
// Parse and execute the templates only for the content portion of the page
|
// Switch the layout which will only render the page content
|
||||||
// The templates used for this partial request will be:
|
page.Layout = templates.LayoutHTMX
|
||||||
// 1. The base htmx template which omits the layout and only includes the content template
|
|
||||||
// 2. The content template specified in Page.Name
|
// Alter the template group so this is cached separately
|
||||||
// 3. All templates within the components directory
|
templateGroup = "page:htmx"
|
||||||
// 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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 {
|
if err != nil {
|
||||||
return c.Fail(err, "failed to parse and execute templates")
|
return c.Fail(err, "failed to parse and execute templates")
|
||||||
}
|
}
|
||||||
|
@ -93,7 +93,7 @@ func TestController_RenderPage(t *testing.T) {
|
|||||||
|
|
||||||
// Check the template cache
|
// Check the template cache
|
||||||
parsed, err := c.TemplateRenderer.Load("page", string(p.Name))
|
parsed, err := c.TemplateRenderer.Load("page", string(p.Name))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Check that all expected templates were parsed.
|
// Check that all expected templates were parsed.
|
||||||
// This includes the name, layout and all components
|
// This includes the name, layout and all components
|
||||||
@ -126,7 +126,7 @@ func TestController_RenderPage(t *testing.T) {
|
|||||||
|
|
||||||
// Check the template cache
|
// Check the template cache
|
||||||
parsed, err := c.TemplateRenderer.Load("page:htmx", string(p.Name))
|
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.
|
// Check that all expected templates were parsed.
|
||||||
// This includes the name, htmx and all components
|
// This includes the name, htmx and all components
|
||||||
|
@ -24,7 +24,7 @@ func TestTemplateRenderer(t *testing.T) {
|
|||||||
Group(group).
|
Group(group).
|
||||||
Key(id).
|
Key(id).
|
||||||
Base("htmx").
|
Base("htmx").
|
||||||
Files("htmx", "pages/error").
|
Files("layouts/htmx", "pages/error").
|
||||||
Directories("components").
|
Directories("components").
|
||||||
Store()
|
Store()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -17,6 +17,7 @@ type (
|
|||||||
const (
|
const (
|
||||||
LayoutMain Layout = "main"
|
LayoutMain Layout = "main"
|
||||||
LayoutAuth Layout = "auth"
|
LayoutAuth Layout = "auth"
|
||||||
|
LayoutHTMX Layout = "htmx"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
Loading…
Reference in New Issue
Block a user