Simplify providing layout component in page
This commit is contained in:
parent
9b057ae87e
commit
c68d82c385
@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/labstack/echo/v4"
|
||||
|
||||
@ -79,15 +78,11 @@ func (h *Auth) Routes(g *echo.Group) {
|
||||
func (h *Auth) ForgotPasswordPage(ctx echo.Context) error {
|
||||
p := page.New(ctx)
|
||||
p.Title = "Forgot password"
|
||||
p.LayoutComponent = layouts.Auth
|
||||
|
||||
f := form.Get[pages.ForgotPasswordForm](ctx)
|
||||
component := pages.ForgotPassword(p, f)
|
||||
|
||||
// TODO: This can be reused
|
||||
p.LayoutComponent = func(content templ.Component) templ.Component {
|
||||
return layouts.Auth(p, content)
|
||||
}
|
||||
|
||||
return h.RenderPageTempl(ctx, p, component)
|
||||
}
|
||||
|
||||
@ -146,15 +141,11 @@ func (h *Auth) ForgotPasswordSubmit(ctx echo.Context) error {
|
||||
func (h *Auth) LoginPage(ctx echo.Context) error {
|
||||
p := page.New(ctx)
|
||||
p.Title = "Log in"
|
||||
p.LayoutComponent = layouts.Auth
|
||||
|
||||
f := form.Get[pages.LoginForm](ctx)
|
||||
component := pages.Login(p, f)
|
||||
|
||||
// TODO: This can be reused
|
||||
p.LayoutComponent = func(content templ.Component) templ.Component {
|
||||
return layouts.Auth(p, content)
|
||||
}
|
||||
|
||||
return h.RenderPageTempl(ctx, p, component)
|
||||
}
|
||||
|
||||
@ -219,15 +210,11 @@ func (h *Auth) Logout(ctx echo.Context) error {
|
||||
func (h *Auth) RegisterPage(ctx echo.Context) error {
|
||||
p := page.New(ctx)
|
||||
p.Title = "Register"
|
||||
p.LayoutComponent = layouts.Auth
|
||||
|
||||
f := form.Get[pages.RegisterForm](ctx)
|
||||
component := pages.Register(p, f)
|
||||
|
||||
// TODO: This can be reused
|
||||
p.LayoutComponent = func(content templ.Component) templ.Component {
|
||||
return layouts.Auth(p, content)
|
||||
}
|
||||
|
||||
return h.RenderPageTempl(ctx, p, component)
|
||||
}
|
||||
|
||||
@ -328,15 +315,11 @@ func (h *Auth) sendVerificationEmail(ctx echo.Context, usr sqlc.User) {
|
||||
func (h *Auth) ResetPasswordPage(ctx echo.Context) error {
|
||||
p := page.New(ctx)
|
||||
p.Title = "Reset password"
|
||||
p.LayoutComponent = layouts.Auth
|
||||
|
||||
f := form.Get[pages.ResetPasswordForm](ctx)
|
||||
component := pages.ResetPassword(p, f)
|
||||
|
||||
// TODO: This can be reused
|
||||
p.LayoutComponent = func(content templ.Component) templ.Component {
|
||||
return layouts.Auth(p, content)
|
||||
}
|
||||
|
||||
return h.RenderPageTempl(ctx, p, component)
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
"github.com/labstack/echo/v4"
|
||||
|
||||
"git.grosinger.net/tgrosinger/saasitone/pkg/form"
|
||||
@ -44,6 +43,7 @@ func (h *Cache) Routes(g *echo.Group) {
|
||||
func (h *Cache) Page(ctx echo.Context) error {
|
||||
p := page.New(ctx)
|
||||
p.Title = "Set a cache entry"
|
||||
p.LayoutComponent = layouts.Main
|
||||
|
||||
// Fetch the value from the cache
|
||||
value, err := h.cache.
|
||||
@ -66,11 +66,6 @@ func (h *Cache) Page(ctx echo.Context) error {
|
||||
f := form.Get[pages.CacheForm](ctx)
|
||||
component := pages.Cache(p, f, valueStrPtr)
|
||||
|
||||
// TODO: This can be reused
|
||||
p.LayoutComponent = func(content templ.Component) templ.Component {
|
||||
return layouts.Main(p, content)
|
||||
}
|
||||
|
||||
return h.RenderPageTempl(ctx, p, component)
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,6 @@ package handlers
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/labstack/echo/v4"
|
||||
|
||||
@ -44,15 +43,11 @@ func (h *Contact) Routes(g *echo.Group) {
|
||||
func (h *Contact) Page(ctx echo.Context) error {
|
||||
p := page.New(ctx)
|
||||
p.Title = "Contact us"
|
||||
p.LayoutComponent = layouts.Main
|
||||
|
||||
f := form.Get[pages.ContactForm](ctx)
|
||||
component := pages.Contact(p, f)
|
||||
|
||||
// TODO: This can be reused
|
||||
p.LayoutComponent = func(content templ.Component) templ.Component {
|
||||
return layouts.Main(p, content)
|
||||
}
|
||||
|
||||
return h.RenderPageTempl(ctx, p, component)
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,6 @@ package handlers
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
"github.com/labstack/echo/v4"
|
||||
|
||||
"git.grosinger.net/tgrosinger/saasitone/pkg/context"
|
||||
@ -42,14 +41,11 @@ func (e *Error) Page(err error, ctx echo.Context) {
|
||||
p := page.New(ctx)
|
||||
p.Title = http.StatusText(code)
|
||||
p.StatusCode = code
|
||||
p.LayoutComponent = layouts.Main
|
||||
p.HTMX.Request.Enabled = false
|
||||
|
||||
component := pages.Error(p)
|
||||
|
||||
p.LayoutComponent = func(content templ.Component) templ.Component {
|
||||
return layouts.Main(p, content)
|
||||
}
|
||||
|
||||
if err = e.RenderPageTempl(ctx, p, component); err != nil {
|
||||
log.Ctx(ctx).Error("failed to render error page",
|
||||
"error", err,
|
||||
|
@ -1,7 +1,6 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"github.com/a-h/templ"
|
||||
"github.com/labstack/echo/v4"
|
||||
|
||||
"git.grosinger.net/tgrosinger/saasitone/pkg/page"
|
||||
@ -42,21 +41,18 @@ func (h *Pages) Home(ctx echo.Context) error {
|
||||
p.Metatags.Description = "Welcome to the homepage."
|
||||
p.Metatags.Keywords = []string{"Go", "MVC", "Web", "Software"}
|
||||
p.Pager = page.NewPager(ctx, 4)
|
||||
p.LayoutComponent = layouts.Main
|
||||
|
||||
data := h.Post.FetchAll(&p.Pager)
|
||||
component := pages.Home(p, data)
|
||||
|
||||
// TODO: This can be reused
|
||||
p.LayoutComponent = func(content templ.Component) templ.Component {
|
||||
return layouts.Main(p, content)
|
||||
}
|
||||
|
||||
return h.RenderPageTempl(ctx, p, component)
|
||||
}
|
||||
|
||||
func (h *Pages) About(ctx echo.Context) error {
|
||||
p := page.New(ctx)
|
||||
p.Title = "About"
|
||||
p.LayoutComponent = layouts.Main
|
||||
|
||||
// This page will be cached!
|
||||
p.Cache.Enabled = true
|
||||
@ -93,11 +89,5 @@ func (h *Pages) About(ctx echo.Context) error {
|
||||
}
|
||||
|
||||
component := pages.About(p, data)
|
||||
|
||||
// TODO: This can be reused
|
||||
p.LayoutComponent = func(content templ.Component) templ.Component {
|
||||
return layouts.Main(p, content)
|
||||
}
|
||||
|
||||
return h.RenderPageTempl(ctx, p, component)
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
"github.com/labstack/echo/v4"
|
||||
|
||||
"git.grosinger.net/tgrosinger/saasitone/pkg/page"
|
||||
@ -36,6 +35,7 @@ func (h *Search) Routes(g *echo.Group) {
|
||||
|
||||
func (h *Search) Page(ctx echo.Context) error {
|
||||
p := page.New(ctx)
|
||||
p.LayoutComponent = layouts.Main
|
||||
|
||||
// Fake search results
|
||||
var results []pages.SearchResult
|
||||
@ -52,11 +52,5 @@ func (h *Search) Page(ctx echo.Context) error {
|
||||
}
|
||||
|
||||
component := pages.Search(p, results)
|
||||
|
||||
// TODO: This can be reused
|
||||
p.LayoutComponent = func(content templ.Component) templ.Component {
|
||||
return layouts.Main(p, content)
|
||||
}
|
||||
|
||||
return h.RenderPageTempl(ctx, p, component)
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/mikestefanello/backlite"
|
||||
@ -48,15 +47,10 @@ func (h *Task) Routes(g *echo.Group) {
|
||||
func (h *Task) Page(ctx echo.Context) error {
|
||||
p := page.New(ctx)
|
||||
p.Title = "Create a task"
|
||||
p.LayoutComponent = layouts.Main
|
||||
|
||||
f := form.Get[pages.TaskForm](ctx)
|
||||
component := pages.Task(p, f)
|
||||
|
||||
// TODO: This can be reused
|
||||
p.LayoutComponent = func(content templ.Component) templ.Component {
|
||||
return layouts.Main(p, content)
|
||||
}
|
||||
|
||||
return h.RenderPageTempl(ctx, p, component)
|
||||
}
|
||||
|
||||
|
@ -5,7 +5,6 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
@ -24,9 +23,7 @@ func TestServeCachedPage(t *testing.T) {
|
||||
p.StatusCode = http.StatusCreated
|
||||
p.Headers["a"] = "b"
|
||||
p.Headers["c"] = "d"
|
||||
p.LayoutComponent = func(content templ.Component) templ.Component {
|
||||
return layouts.HTMX(p, content)
|
||||
}
|
||||
p.LayoutComponent = layouts.HTMX
|
||||
err := c.TemplateRenderer.RenderPageTempl(ctx, p, pages.Cache(p, &pages.CacheForm{}, nil))
|
||||
output := rec.Body.Bytes()
|
||||
require.NoError(t, err)
|
||||
|
@ -41,7 +41,7 @@ type Page struct {
|
||||
// ToURL is a function to convert a route name and optional route parameters to a URL
|
||||
ToURL func(name string, params ...interface{}) string
|
||||
|
||||
LayoutComponent func(content templ.Component) templ.Component
|
||||
LayoutComponent func(p Page, content templ.Component) templ.Component
|
||||
|
||||
// IsHome stores whether the requested page is the home page or not
|
||||
IsHome bool
|
||||
|
@ -110,7 +110,7 @@ func (t *TemplateRenderer) RenderPageTempl(ctx echo.Context, page page.Page, con
|
||||
// Only partial content should be rendered.
|
||||
err = content.Render(ctx.Request().Context(), &buf)
|
||||
} else {
|
||||
err = page.LayoutComponent(content).Render(ctx.Request().Context(), &buf)
|
||||
err = page.LayoutComponent(page, content).Render(ctx.Request().Context(), &buf)
|
||||
}
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(
|
||||
|
@ -5,7 +5,6 @@ import (
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
@ -87,16 +86,13 @@ func TestTemplateRenderer_RenderPage(t *testing.T) {
|
||||
|
||||
t.Run("htmx rendering", func(t *testing.T) {
|
||||
ctx, _, p := setup()
|
||||
p.LayoutComponent = layouts.Main
|
||||
p.HTMX.Request.Enabled = true
|
||||
p.HTMX.Response = &htmx.Response{
|
||||
Trigger: "trigger",
|
||||
}
|
||||
|
||||
component := pages.Home(p, []models.Post{})
|
||||
p.LayoutComponent = func(content templ.Component) templ.Component {
|
||||
return layouts.Main(p, content)
|
||||
}
|
||||
|
||||
err := c.TemplateRenderer.RenderPageTempl(ctx, p, component)
|
||||
require.NoError(t, err)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user