Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information