Skip to content

Commit

Permalink
changelist:
Browse files Browse the repository at this point in the history
- don't override component name if already set
- option to skip rendering
  • Loading branch information
yznts committed Apr 18, 2024
1 parent f7b3447 commit 98ff829
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
10 changes: 8 additions & 2 deletions rendering/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions rendering/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions rendering/renderer.template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down

0 comments on commit 98ff829

Please sign in to comment.