From cde62528d07a6196ba683a34a1d048e2da8aa205 Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Wed, 22 Nov 2023 07:12:19 -0800 Subject: [PATCH 1/2] Add None Node for Noop Rendering --- elem.go | 13 +++++++++++++ elements.go | 5 +++++ elements_test.go | 13 +++++++++++++ utils_test.go | 10 ++++++++++ 4 files changed, 41 insertions(+) diff --git a/elem.go b/elem.go index a1ce79d..75ac219 100644 --- a/elem.go +++ b/elem.go @@ -58,6 +58,19 @@ type Node interface { Render() string } +// NoneNode represents a node that renders nothing. +type NoneNode struct{} + +// RenderTo for NoneNode does nothing. +func (n NoneNode) RenderTo(builder *strings.Builder) { + // Intentionally left blank to render nothing +} + +// Render for NoneNode returns an empty string. +func (n NoneNode) Render() string { + return "" +} + type TextNode string func (t TextNode) RenderTo(builder *strings.Builder) { diff --git a/elements.go b/elements.go index b4db73a..69f9fe9 100644 --- a/elements.go +++ b/elements.go @@ -399,6 +399,11 @@ func Source(attrs attrs.Props, children ...Node) *Element { // ========== Other ========== +// None creates a NoneNode, representing a no-operation in rendering. +func None() NoneNode { + return NoneNode{} +} + // Raw takes html content and returns a RawNode. func Raw(html string) RawNode { return RawNode(html) diff --git a/elements_test.go b/elements_test.go index 1ea166c..bb88db8 100644 --- a/elements_test.go +++ b/elements_test.go @@ -509,6 +509,19 @@ func TestVideoWithSourceElementsAndFallbackText(t *testing.T) { // ========== Other ========== +func TestNone(t *testing.T) { + el := None() + expected := "" + + assert.Equal(t, expected, el.Render(), "None should render an empty string") +} + +func TestNoneInDiv(t *testing.T) { + expected := `
` + actual := Div(nil, None()).Render() + assert.Equal(t, expected, actual) +} + func TestRaw(t *testing.T) { rawHTML := `

Test paragraph

` el := Raw(rawHTML) diff --git a/utils_test.go b/utils_test.go index aaf6007..ce281d3 100644 --- a/utils_test.go +++ b/utils_test.go @@ -17,6 +17,16 @@ func TestIf(t *testing.T) { assert.Equal(t, falseElement.Render(), resultFalse.Render()) } +func TestIfUsingNone(t *testing.T) { + trueElement := Div(nil, Text("True Condition")) + + resultWithNone := If[Node](true, trueElement, None()) + assert.Equal(t, trueElement.Render(), resultWithNone.Render(), "If should render the true element when the condition is true") + + resultWithNoneFalse := If[Node](false, trueElement, None()) + assert.Equal(t, "", resultWithNoneFalse.Render(), "If should render nothing (empty string) when the condition is false and None is used") +} + func TestTransformEach(t *testing.T) { items := []string{"Item 1", "Item 2", "Item 3"} From fd002e38106339d0222462a4991dc746b314d451 Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Mon, 27 Nov 2023 16:38:51 -0800 Subject: [PATCH 2/2] Add `None` to Readme --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 0f53b3d..c59ed8e 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,23 @@ content := elem.Div(nil, In this example, if `isAdmin` is `true`, the `Admin Panel` link is rendered. Otherwise, the `Login` link is rendered. +#### `None` in Conditional Rendering + +`elem` provides a specialized node `None` that implements the `Node` interface but does not produce any visible output. It's particularly useful in scenarios where rendering nothing for a specific condition is required. + +```go +showWelcomeMessage := false +welcomeMessage := elem.Div(nil, elem.Text("Welcome to our website!")) +// Using NoneNode for conditional rendering +content := elem.Div(nil, + elem.If[elem.Node](showWelcomeMessage, welcomeMessage, elem.None()), +) +``` + +In this example, `welcomeMessage` is rendered only if `showWelcomeMessage` is `true`. If it's `false`, `None` is rendered instead, which produces no visible output. + +Additionally, `None` can be used to create an empty element, as in `elem.Div(nil, elem.None())`, which results in `
`. This can be handy for creating placeholders or structuring your HTML document without adding additional content. + ### Supported Elements `elem` provides utility functions for creating HTML elements: