-
-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why can't a group be rendered? #154
Comments
An alternative that i'd also be excited about is to export the |
Hi @ddouglas. Thank you for raising this issue! Basically, it's because rendering a group of I don't know about exporting it, I really like to keep the core API surface area minimal. Would you be able to achieve something similar with nested |
Thanks for clearing this up. That makes sense. Unfortunately, wrapping it in a div violates the requirements of performing an out of band swap has each id that needs to be swapped must be a top level element, but returning multiple "top level" elements that aren't wrapped by a div still satisfies this contract:
Regardless, we (i) don't expect you to make sweeping changes to the lib because HTMX is picking up steam this year. Thank you for this awesome lib. |
@ddouglas I'm glad you found a solution. I'm an HTMX fan myself, so maybe there's room for some helpers over at https://github.com/maragudk/gomponents-htmx 😊 . Let me know if you think of something. |
I ran into exactly the same problem when trying to make pagination with HTMX, I made a help function for whoever needs it package components
import (
"bytes"
"github.com/maragudk/gomponents"
)
// RenderableGroup renders a group of nodes without a parent element.
//
// This is because gomponents.Group() cannot be directly rendered and
// needs to be wrapped in a parent element.
func RenderableGroup(children []gomponents.Node) gomponents.Node {
buf := bytes.Buffer{}
for _, child := range children {
err := child.Render(&buf)
if err != nil {
return gomponents.Raw("Error rendering group")
}
}
return gomponents.Raw(buf.String())
} And the tests: package components
import (
"bytes"
"testing"
"github.com/maragudk/gomponents"
"github.com/maragudk/gomponents/html"
"github.com/stretchr/testify/assert"
)
func TestRenderableGroupRenderer(t *testing.T) {
t.Run("renders a group of string nodes without a parent element", func(t *testing.T) {
gotRenderer := RenderableGroup([]gomponents.Node{
gomponents.Text("foo"),
gomponents.Text("bar"),
})
got := bytes.Buffer{}
err := gotRenderer.Render(&got)
assert.NoError(t, err)
expected := "foobar"
assert.Equal(t, expected, got.String())
})
t.Run("renders a group of tag nodes without a parent element", func(t *testing.T) {
gotRenderer := RenderableGroup([]gomponents.Node{
html.Span(
gomponents.Text("foo"),
),
html.P(
gomponents.Text("bar"),
),
})
got := bytes.Buffer{}
err := gotRenderer.Render(&got)
assert.NoError(t, err)
expected := `<span>foo</span><p>bar</p>`
assert.Equal(t, expected, got.String())
})
} |
I ended up doing something like what @eduardolat did. I wish we'd have a |
@JulienTant agreed. I often wish for this myself in my code, because I use HTMX myself. I think I just want to make |
@markuswustenberg 1 to make group renderable disclaimer |
See #181. |
Personally I like to assemble my fragments at the handler, which means I do something like this: page.Question(question.Prompt,
components.QuestionToInputMethod(question, questionSession),
feedback,
questionURLHTMX(quiz.ID, question.Position-1, len(questions)),
questionURLHTMX(quiz.ID, question.Position 1, len(questions)),
questionURL(quiz.ID, question.Position, len(questions)),
).Render(w)
page.Sidebar(true, sidebar(true, quiz.ID, questionSessions, questions), summaryURL(quiz.ID)).Render(w)
components.Excerpt(true, excerpt, question.Reference).Render(w) I just render out several elements onto the ResponseWriter, which is pretty much the equivalent of a fragment. Plus it lets me be more versatile in which fragments I respond with (yes the code needs some refactoring but the idea is there) |
@amrojjeh Ah, interesting approach! I hadn't thought of that. I use the HTTP handler adapter extensively, and barely ever render directly, so for me the |
Hello,
I use gocomponents with HTMX for my hobby sites and I'm currently working on a POC for a project at work.
I noticed today that the
Group
type satisfies the Node interface, but it cannot itself be directory rendered.I'd like to be able to use this to send multiple groups elements in a response for HTMX to perform an out of band swaps and the group method would allow this by looping over the nodes and writing them to the provided
io.Writer
I'm more writing to ask what prompted this functionality to be designed to panic instead of rendering when the lib was written and is the advised work around to just write a function that loops and calls the Render func?
The text was updated successfully, but these errors were encountered: