Added HTMX partial support template rendering.
This commit is contained in:
parent
f115fcb602
commit
b2f64b62f4
7 changed files with 126 additions and 60 deletions
|
|
@ -36,10 +36,13 @@ func NewController(c *services.Container) Controller {
|
|||
|
||||
// RenderPage renders a Page as an HTTP response
|
||||
func (c *Controller) RenderPage(ctx echo.Context, page Page) error {
|
||||
var buf *bytes.Buffer
|
||||
var err error
|
||||
|
||||
// Page name is required
|
||||
if page.Name == "" {
|
||||
ctx.Logger().Error("page render failed due to missing name")
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Internal server error")
|
||||
return echo.NewHTTPError(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
// Use the app name in configuration if a value was not set
|
||||
|
|
@ -47,38 +50,65 @@ func (c *Controller) RenderPage(ctx echo.Context, page Page) error {
|
|||
page.AppName = c.Container.Config.App.Name
|
||||
}
|
||||
|
||||
// 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.ParseAndExecute(
|
||||
"controller",
|
||||
page.Name,
|
||||
page.Layout,
|
||||
[]string{
|
||||
fmt.Sprintf("layouts/%s", page.Layout),
|
||||
fmt.Sprintf("pages/%s", page.Name),
|
||||
},
|
||||
[]string{"components"},
|
||||
page,
|
||||
)
|
||||
// Check if this is an HTMX request
|
||||
if page.HTMX.Request.Enabled && !page.HTMX.Request.Boosted {
|
||||
// Disable caching
|
||||
page.Cache.Enabled = false
|
||||
|
||||
// Parse and execute
|
||||
buf, err = c.Container.TemplateRenderer.ParseAndExecute(
|
||||
"page:htmx",
|
||||
page.Name,
|
||||
"htmx",
|
||||
[]string{
|
||||
"htmx",
|
||||
fmt.Sprintf("pages/%s", page.Name),
|
||||
},
|
||||
[]string{"components"},
|
||||
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.ParseAndExecute(
|
||||
"page",
|
||||
page.Name,
|
||||
page.Layout,
|
||||
[]string{
|
||||
fmt.Sprintf("layouts/%s", page.Layout),
|
||||
fmt.Sprintf("pages/%s", page.Name),
|
||||
},
|
||||
[]string{"components"},
|
||||
page,
|
||||
)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
ctx.Logger().Errorf("failed to parse and execute templates: %v", err)
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Internal server error")
|
||||
return echo.NewHTTPError(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
// Cache this page, if caching was enabled
|
||||
c.cachePage(ctx, page, buf)
|
||||
// Set the status code
|
||||
ctx.Response().Status = page.StatusCode
|
||||
|
||||
// Set any headers
|
||||
for k, v := range page.Headers {
|
||||
ctx.Response().Header().Set(k, v)
|
||||
}
|
||||
|
||||
return ctx.HTMLBlob(page.StatusCode, buf.Bytes())
|
||||
// Apply the HTMX response, if one
|
||||
if page.HTMX.Response != nil {
|
||||
page.HTMX.Response.Apply(ctx)
|
||||
}
|
||||
|
||||
// Cache this page, if caching was enabled
|
||||
c.cachePage(ctx, page, buf)
|
||||
|
||||
return ctx.HTMLBlob(ctx.Response().Status, buf.Bytes())
|
||||
}
|
||||
|
||||
// cachePage caches the HTML for a given Page if the Page has caching enabled
|
||||
|
|
@ -92,6 +122,12 @@ func (c *Controller) cachePage(ctx echo.Context, page Page, html *bytes.Buffer)
|
|||
page.Cache.Expiration = c.Container.Config.Cache.Expiration.Page
|
||||
}
|
||||
|
||||
// Extract the headers
|
||||
headers := make(map[string]string)
|
||||
for k, v := range ctx.Response().Header() {
|
||||
headers[k] = v[0]
|
||||
}
|
||||
|
||||
// The request URL is used as the cache key so the middleware can serve the
|
||||
// cached page on matching requests
|
||||
key := ctx.Request().URL.String()
|
||||
|
|
@ -102,8 +138,8 @@ func (c *Controller) cachePage(ctx echo.Context, page Page, html *bytes.Buffer)
|
|||
cp := middleware.CachedPage{
|
||||
URL: key,
|
||||
HTML: html.Bytes(),
|
||||
Headers: page.Headers,
|
||||
StatusCode: page.StatusCode,
|
||||
Headers: headers,
|
||||
StatusCode: ctx.Response().Status,
|
||||
}
|
||||
|
||||
err := marshaler.New(c.Container.Cache).Set(ctx.Request().Context(), key, cp, opts)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue