diff options
author | Julien Tant | 2024-06-19 01:43:52 -0700 |
---|---|---|
committer | GitHub | 2024-06-19 10:43:52 +0200 |
commit | dafb3daa12160c85639fedd3eacf5aaf51127c53 (patch) | |
tree | 3070ec7a898edb927c77861f1747d1d06a7f9068 /gomponents.go | |
parent | a75b25333d94bef710bbc05a95baa2e77b61bca9 (diff) | |
download | gomponents-dafb3daa12160c85639fedd3eacf5aaf51127c53.tar.lz gomponents-dafb3daa12160c85639fedd3eacf5aaf51127c53.tar.zst gomponents-dafb3daa12160c85639fedd3eacf5aaf51127c53.zip |
Add an `Iff` helper (#172)
I ran into some situation where I want to conditionally render a node if some variable is not nil and obviously I got a panic ```go // package viewmodels type SomePage struct { s *string } // package views func SomePage (vm viewmodels.SomePage) g,Node { return Div( If(vm.s == nil, Text("s is nil"), If(vm.s !- nil, Text("s is " + vm.s), // this will panic when `s` is nil ) } ``` In this situation, go will interpret the code of the second `if` regardless of the condition because the code itself is not in a condition. This PR introduces a new `Iff` helper that accepts a callback. The callback content is only interpreted when it's called, making the code safe: ```go // package viewmodels type SomePage struct { s *string } // package views func SomePage (vm viewmodels.SomePage) g,Node { return Div( Iff(vm.s == nil, func () g.Node { return Text("s is nil") }, Iff(vm.s !- nil, func () g.Node { return Text("s is " + vm.s) }, ) } ``` I'm aware of the `Lazy` effort on the side, but I guess this is no a breaking change and can still exist in addition to the `Lazy` effort. Co-authored-by: Markus Wüstenberg <markus@maragu.dk>
Diffstat (limited to 'gomponents.go')
-rw-r--r-- | gomponents.go | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/gomponents.go b/gomponents.go index 7b73a3b..ae7cadc 100644 --- a/gomponents.go +++ b/gomponents.go @@ -254,9 +254,20 @@ 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. 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. +func Iff(condition bool, f func() Node) Node { + if condition { + return f() + } + return nil +} |