maragudk / gomponents

HTML components in pure Go.
https://www.gomponents.com
MIT License
1.3k stars 32 forks source link

Investigate supporting `Render(context.Context, io.Writer) error` #219

Closed markuswustenberg closed 1 month ago

markuswustenberg commented 1 month ago

Suggested by the article mentioned in #218.

markuswustenberg commented 1 month ago

@EwenQuim could you elaborate what this means? How would you use it?

The way I build components, they look like this:

func SomeComponent(ctx SomeContext) Node {
    return Div(Span(ctx.Whatever)) 
}

In that case, I can pass whatever to it in SomeContext, or other parameters. When would you need the context.Context at render time?

amrojjeh commented 1 month ago

The main argument that's provided for this request is to simplify implementing both Templ and Gomponents, but I personally don't see why this would be done at all, and even if it is, there's no friction as you'd just call the appropriate Render function and pass the required parameters. Any further needed abstractions can be made by the user as per the project requirements

EwenQuim commented 1 month ago

As Adrien said :

Gomponent to Templ conversion

type Gomponent interface {
  Render(w io.Writer) error
}

func ToTempl(g Gomponent) templ.Component {
  func templ.ComponentFunc(func(ctx context.Context, w io.Writer) error {
     return g.Render(w)
  })
}

Templ to Gomponent conversion

type gomp struct {
   c templ.Component
}

func (g gomp) Render(w io.Writer) error {
  return g.c.Render(context.Background(), w)
}

func ToGomponent(c templ.Component) Gomponent {
  return g{c: c}
}

I think we can close the issue

markuswustenberg commented 1 month ago

Thanks for your input @amrojjeh and @EwenQuim. I'll just close the issue. 😊