From b5ab6deaa00b75cc9d077bfb27d19ab584d1f822 Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Fri, 13 Oct 2023 08:46:37 -0700 Subject: [PATCH] Pass the builder in RenderTo --- elem.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/elem.go b/elem.go index 13d1b53..b42249b 100644 --- a/elem.go +++ b/elem.go @@ -35,9 +35,7 @@ type Element struct { Children []interface{} // Can be either string (for text) or another Element } -func (e *Element) Render() string { - var builder strings.Builder - +func (e *Element) RenderTo(builder *strings.Builder) { // Start with opening tag builder.WriteString("<") builder.WriteString(e.Tag) @@ -61,7 +59,7 @@ func (e *Element) Render() string { // If it's a void element, close it and return if _, exists := voidElements[e.Tag]; exists { builder.WriteString(`>`) - return builder.String() + return } // Close opening tag @@ -73,7 +71,7 @@ func (e *Element) Render() string { case string: builder.WriteString(c) case *Element: - builder.WriteString(c.Render()) + c.RenderTo(builder) } } @@ -81,7 +79,11 @@ func (e *Element) Render() string { builder.WriteString(``) +} +func (e *Element) Render() string { + var builder strings.Builder + e.RenderTo(&builder) return builder.String() }