-
The interface for componet is: type Component interface {
// Render the template.
Render(ctx context.Context, w io.Writer) error
} but when writing a template the syntax is:
What if I want
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
The idea was that you'd have created a view model already, and wouldn't be doing logic that can fail inside a template, just like http.Handler doesn't return an error. The idea of view models is briefly touched on in https://templ.guide/core-concepts/view-models. In your case I'd make a model that doesn't use the iterator directly, but instead, copies the results to a slice for display.
In a code component, you can do whatever you want, of course. 😀 |
Beta Was this translation helpful? Give feedback.
-
@a-h this made me look at the code that templ generates and I was disappointed to find its fully buffering the response rather than writing a the response writer (or a bufio.Writer around it). This means that I can't use templ for large streaming requests. I also realized that
doesn't check that b.Cap() is huge and intentionally not return it to the pool and allow it to be GC'd. Reading the code it looks like I can work around the last issue at least by passing my own *bytes.Buffer in as the destination. Thanks for the quick reply. |
Beta Was this translation helpful? Give feedback.
The idea was that you'd have created a view model already, and wouldn't be doing logic that can fail inside a template, just like http.Handler doesn't return an error.
The idea of view models is briefly touched on in https://templ.guide/core-concepts/view-models.
In your case I'd make a model that doesn't use the iterator directly, but instead, copies the results to a slice for display.
templ x() {
components basically elide the implicit return. When Go getsyield
statements, maybe it will be time to reconsider it, since there will be more options.In a code component, you can do whatever you want, of course. 😀