From 98ff829f3220281a4ba1f22d1c3156c7dea92be3 Mon Sep 17 00:00:00 2001 From: Yurii Zinets Date: Thu, 18 Apr 2024 23:32:11 +0200 Subject: [PATCH] changelist: - don't override component name if already set - option to skip rendering --- rendering/handler.go | 10 ++++++++-- rendering/renderer.go | 3 +++ rendering/renderer.template.go | 5 +++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/rendering/handler.go b/rendering/handler.go index 85be347..c7a9f12 100644 --- a/rendering/handler.go +++ b/rendering/handler.go @@ -13,12 +13,18 @@ func Handler(c component.Component) http.HandlerFunc { ctx := component.NewContext(w, r) // Build page state tree state := c(ctx) - // Inject component name - state.SetName(c.GetName()) + // Inject component name, unless it's already set + if state.GetName() == "" { + state.SetName(c.GetName()) + } // Ensure state implements render if _, ok := state.(Renderer); !ok { panic("The component does not implement rendering") } + // Check if we need to skip rendering + if state.(Renderer).RenderSkip() { + return + } // Render if err := state.(Renderer).Render(state, ctx.ResponseWriter); err != nil { panic(err) diff --git a/rendering/renderer.go b/rendering/renderer.go index 7d03c70..545ccd2 100644 --- a/rendering/renderer.go +++ b/rendering/renderer.go @@ -8,6 +8,9 @@ import ( // Renderer defines requirements for rendering implementations. type Renderer interface { + // Define if rendering must to be skipped. + // Needed for cases like redirects. + RenderSkip() bool // Render component into io.Writer. Render(state component.State, out io.Writer) error } diff --git a/rendering/renderer.template.go b/rendering/renderer.template.go index 2f1795e..e97c986 100644 --- a/rendering/renderer.template.go +++ b/rendering/renderer.template.go @@ -22,12 +22,17 @@ var ( type Template struct { Raw *template.Template `json:"-"` // Raw template will be used instead if provided Name string // Resolved from component name by default + Skip bool `json:"-"` // false by default Glob string `json:"-"` // *.html by default EmbedFS *embed.FS `json:"-"` // nil by default FuncMap template.FuncMap `json:"-"` // render.FuncMap by default } +func (t *Template) RenderSkip() bool { + return t.Skip +} + func (t *Template) Render(state component.State, w io.Writer) error { // Defaults if t.Name == "" {