From 6c8f0c235287edf7252fe239d4c9beb258c6ff01 Mon Sep 17 00:00:00 2001 From: Markus Wüstenberg Date: Mon, 2 Nov 2020 10:03:05 +0100 Subject: Render to Writer instead of string (#39) The Render function has been changed to take a `Writer` instead of returning a string. This makes it possible to generate documents without having the whole content in memory. This also removes the `gomponents.Write` function, which is now redundant. Furthermore, the `el.Document` function has been changed to only take one child, as multiple children never make sense for it. (It's not even a child, more a sibling.)--- el/elements.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'el/elements.go') diff --git a/el/elements.go b/el/elements.go index 4ca001f..0841ff8 100644 --- a/el/elements.go +++ b/el/elements.go @@ -4,7 +4,7 @@ package el import ( "fmt" - "strings" + "io" g "github.com/maragudk/gomponents" ) @@ -13,15 +13,13 @@ func A(href string, children ...g.Node) g.NodeFunc { return g.El("a", g.Attr("href", href), g.Group(children)) } -// Document returns an special kind of Node that prefixes its children with the string "". -func Document(children ...g.Node) g.NodeFunc { - return func() string { - var b strings.Builder - b.WriteString("") - for _, c := range children { - b.WriteString(c.Render()) +// Document returns an special kind of Node that prefixes its child with the string "". +func Document(child g.Node) g.NodeFunc { + return func(w io.Writer) error { + if _, err := w.Write([]byte("")); err != nil { + return err } - return b.String() + return child.Render(w) } } -- cgit 1.4.1