diff options
author | Markus Wüstenberg | 2024-06-25 13:46:24 +0200 |
---|---|---|
committer | GitHub | 2024-06-25 13:46:24 +0200 |
commit | 1c0ceb44a530632be2d591eafcadea2ba481a682 (patch) | |
tree | b61cc5295dd71ea337ed118a0834e81a80c3dd7d | |
parent | d9708f9290f723dd4424f86d300b18974e2c169b (diff) | |
download | gomponents-1c0ceb44a530632be2d591eafcadea2ba481a682.tar.lz gomponents-1c0ceb44a530632be2d591eafcadea2ba481a682.tar.zst gomponents-1c0ceb44a530632be2d591eafcadea2ba481a682.zip |
Adjust documentation on Iff (#179)
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | gomponents.go | 10 | ||||
-rw-r--r-- | gomponents_test.go | 14 |
3 files changed, 17 insertions, 9 deletions
diff --git a/README.md b/README.md index bef2fbf..744b104 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Made in 🇩🇰 by [maragu](https://www.maragu.dk), maker of [online Go courses - Nice formatting with `gofmt` - Simple API that's easy to learn and use (you know most already if you know HTML) - Useful helpers like `Text` and `Textf` that insert HTML-escaped text, `Map` for mapping data to components, - and `If` for conditional rendering. + and `If`/`Iff` for conditional rendering. - No external dependencies ## Usage diff --git a/gomponents.go b/gomponents.go index ae7cadc..d93c8ae 100644 --- a/gomponents.go +++ b/gomponents.go @@ -254,8 +254,8 @@ func Group(children []Node) Node { // If condition is true, return the given Node. Otherwise, return nil. // This helper function is good for inlining elements conditionally. -// If your condition and node involve a nilable variable, use iff because -// go will evaluate the node regardless of the condition. +// If it's important that the given Node is only evaluated if condition is true +// (for example, when using nilable variables), use [Iff] instead. func If(condition bool, n Node) Node { if condition { return n @@ -263,8 +263,10 @@ func If(condition bool, n Node) Node { return nil } -// Iff execute the function f if condition is true, otherwise return nil. -// it is the preferred way to conditionally render a node if the node involves a nilable variable. +// Iff condition is true, call the given function. Otherwise, return nil. +// This helper function is good for inlining elements conditionally when the node depends on nilable data, +// or some other code that could potentially panic. +// If you just need simple conditional rendering, see [If]. func Iff(condition bool, f func() Node) Node { if condition { return f() diff --git a/gomponents_test.go b/gomponents_test.go index df5da4f..c0ae7a7 100644 --- a/gomponents_test.go +++ b/gomponents_test.go @@ -277,10 +277,12 @@ func TestIf(t *testing.T) { func ExampleIf() { showMessage := true + e := g.El("div", g.If(showMessage, g.El("span", g.Text("You lost your hat!"))), g.If(!showMessage, g.El("span", g.Text("No messages."))), ) + _ = e.Render(os.Stdout) // Output: <div><span>You lost your hat!</span></div> } @@ -302,14 +304,18 @@ func TestIff(t *testing.T) { } func ExampleIff() { - var nillableVariable *struct { - str string + type User struct { + Name string } + var user *User + e := g.El("div", - g.Iff(nillableVariable != nil, func() g.Node { - return g.Text(nillableVariable.str) + // This would panic using just If + g.Iff(user != nil, func() g.Node { + return g.Text(user.Name) }), ) + _ = e.Render(os.Stdout) // Output: <div></div> } |