Skip to content

Commit

Permalink
Adjust documentation on Iff (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
markuswustenberg authored Jun 25, 2024
1 parent d9708f9 commit 1c0ceb4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions gomponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,17 +254,19 @@ 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
}
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()
Expand Down
14 changes: 10 additions & 4 deletions gomponents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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>
}
Expand All @@ -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>
}

0 comments on commit 1c0ceb4

Please sign in to comment.