htmgo/framework/h/conditionals.go

67 lines
1 KiB
Go
Raw Normal View History

2024-09-21 03:59:07 +00:00
package h
func If(condition bool, node Ren) Ren {
if condition {
return node
} else {
return Empty()
}
}
2024-09-21 16:52:56 +00:00
func Ternary[T any](value bool, a T, b T) T {
2024-09-22 16:25:18 +00:00
return IfElse(value, a, b)
2024-09-21 16:52:56 +00:00
}
2024-09-23 15:57:59 +00:00
func ElementIf(condition bool, element *Element) *Element {
if condition {
return element
} else {
return Empty()
}
}
2024-09-28 02:29:53 +00:00
func IfElseE(condition bool, element *Element, element2 *Element) *Element {
if condition {
return element
} else {
return element2
}
}
2024-09-22 16:25:18 +00:00
func IfElse[T any](condition bool, node T, node2 T) T {
2024-09-21 03:59:07 +00:00
if condition {
return node
} else {
return node2
}
}
2024-09-22 16:25:18 +00:00
func IfElseLazy[T any](condition bool, cb1 func() T, cb2 func() T) T {
2024-09-21 03:59:07 +00:00
if condition {
return cb1()
} else {
return cb2()
}
}
func IfHtmxRequest(ctx *RequestContext, node Ren) Ren {
if ctx.isHxRequest {
2024-09-21 03:59:07 +00:00
return node
}
return Empty()
}
2024-09-21 16:52:56 +00:00
func ClassIf(condition bool, value string) Ren {
if condition {
return Class(value)
}
return Empty()
}
2024-09-24 19:51:46 +00:00
func AttributeIf(condition bool, name string, value string) Ren {
if condition {
return Attribute(name, value)
}
return Empty()
}