Skip to content
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

Add None Node for Noop Rendering #89

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<div></div>`. 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:
Expand Down
13 changes: 13 additions & 0 deletions elem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions elements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := `<div></div>`
actual := Div(nil, None()).Render()
assert.Equal(t, expected, actual)
}

func TestRaw(t *testing.T) {
rawHTML := `<div class="test"><p>Test paragraph</p></div>`
el := Raw(rawHTML)
Expand Down
10 changes: 10 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

Expand Down