From 792707cd704071b4699150819d1aa1634d7f3128 Mon Sep 17 00:00:00 2001 From: Tony Grosinger Date: Wed, 17 Jul 2024 20:10:04 -0700 Subject: [PATCH] Convert cache page to Templ --- pkg/handlers/cache.go | 27 +++++---- templ/pages/cache.templ | 46 +++++++++++++++ templ/pages/cache_templ.go | 108 +++++++++++++++++++++++++++++++++++ templates/pages/cache.gohtml | 36 ------------ 4 files changed, 171 insertions(+), 46 deletions(-) create mode 100644 templ/pages/cache.templ create mode 100644 templ/pages/cache_templ.go delete mode 100644 templates/pages/cache.gohtml diff --git a/pkg/handlers/cache.go b/pkg/handlers/cache.go index e3dac22..4078d33 100644 --- a/pkg/handlers/cache.go +++ b/pkg/handlers/cache.go @@ -4,11 +4,14 @@ import ( "errors" "time" + "github.com/a-h/templ" "github.com/labstack/echo/v4" "git.grosinger.net/tgrosinger/saasitone/pkg/form" "git.grosinger.net/tgrosinger/saasitone/pkg/page" "git.grosinger.net/tgrosinger/saasitone/pkg/services" + "git.grosinger.net/tgrosinger/saasitone/templ/layouts" + "git.grosinger.net/tgrosinger/saasitone/templ/pages" "git.grosinger.net/tgrosinger/saasitone/templates" ) @@ -22,11 +25,6 @@ type ( cache *services.CacheClient *services.TemplateRenderer } - - cacheForm struct { - Value string `form:"value"` - form.Submission - } ) func init() { @@ -46,10 +44,8 @@ func (h *Cache) Routes(g *echo.Group) { func (h *Cache) Page(ctx echo.Context) error { p := page.New(ctx) - p.Layout = templates.LayoutMain p.Name = templates.PageCache p.Title = "Set a cache entry" - p.Form = form.Get[cacheForm](ctx) // Fetch the value from the cache value, err := h.cache. @@ -57,20 +53,31 @@ func (h *Cache) Page(ctx echo.Context) error { Key("page_cache_example"). Fetch(ctx.Request().Context()) + var valueStrPtr *string = nil + // Store the value in the page, so it can be rendered, if found switch { case err == nil: - p.Data = value.(string) + valueStr := value.(string) + valueStrPtr = &valueStr case errors.Is(err, services.ErrCacheMiss): default: return fail(err, "failed to fetch from cache") } - return h.RenderPage(ctx, p) + 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) } func (h *Cache) Submit(ctx echo.Context) error { - var input cacheForm + var input pages.CacheForm if err := form.Submit(ctx, &input); err != nil { return err diff --git a/templ/pages/cache.templ b/templ/pages/cache.templ new file mode 100644 index 0000000..ea0fd0a --- /dev/null +++ b/templ/pages/cache.templ @@ -0,0 +1,46 @@ +package pages + +import ( + "git.grosinger.net/tgrosinger/saasitone/templ/components" + "git.grosinger.net/tgrosinger/saasitone/pkg/form" + "git.grosinger.net/tgrosinger/saasitone/pkg/page" +) + +type CacheForm struct { + Value string `form:"value"` + form.Submission +} + +templ Cache(p page.Page, f *CacheForm, value *string) { +
+
+
+

Test the cache

+
+
+ This route handler shows how the default in-memory cache works. Try updating the value using the form below and see how it persists after you reload the page. + HTMX makes it easy to re-render the cached value after the form is submitted. +
+
+ + if value != nil { + { *value } + } else { + (empty) + } +
+
+
+ +
+ +
+
+
+
+ +
+
+ @components.CSRF(p.CSRF) +
+} diff --git a/templ/pages/cache_templ.go b/templ/pages/cache_templ.go new file mode 100644 index 0000000..799b97f --- /dev/null +++ b/templ/pages/cache_templ.go @@ -0,0 +1,108 @@ +// Code generated by templ - DO NOT EDIT. + +// templ: version: v0.2.707 +package pages + +//lint:file-ignore SA4006 This context is only used if a nested component is present. + +import "github.com/a-h/templ" +import "context" +import "io" +import "bytes" + +import ( + "git.grosinger.net/tgrosinger/saasitone/pkg/form" + "git.grosinger.net/tgrosinger/saasitone/pkg/page" + "git.grosinger.net/tgrosinger/saasitone/templ/components" +) + +type CacheForm struct { + Value string `form:"value"` + form.Submission +} + +func Cache(p page.Page, f *CacheForm, value *string) templ.Component { + return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) + if !templ_7745c5c3_IsBuffer { + templ_7745c5c3_Buffer = templ.GetBuffer() + defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var1 := templ.GetChildren(ctx) + if templ_7745c5c3_Var1 == nil { + templ_7745c5c3_Var1 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

Test the cache

This route handler shows how the default in-memory cache works. Try updating the value using the form below and see how it persists after you reload the page. HTMX makes it easy to re-render the cached value after the form is submitted.
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + if value != nil { + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var3 string + templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(*value) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `templ/pages/cache.templ`, Line: 27, Col: 40} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } else { + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("(empty)") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = components.CSRF(p.CSRF).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + if !templ_7745c5c3_IsBuffer { + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) + } + return templ_7745c5c3_Err + }) +} diff --git a/templates/pages/cache.gohtml b/templates/pages/cache.gohtml deleted file mode 100644 index a0bb0af..0000000 --- a/templates/pages/cache.gohtml +++ /dev/null @@ -1,36 +0,0 @@ -{{define "content"}} -
-
-
-

Test the cache

-
-
- This route handler shows how the default in-memory cache works. Try updating the value using the form below and see how it persists after you reload the page. - HTMX makes it easy to re-render the cached value after the form is submitted. -
-
- - - {{if .Data}} - {{.Data}} - {{- else}} - (empty) - {{- end}} -

- -
- -
- -
-
- -
-
- -
-
- - {{template "csrf" .}} -
-{{end}} \ No newline at end of file