Skip to main content

Switch

templ uses standard Go switch statements which can be used to conditionally render components and elements.

component.templ
package main

templ userTypeDisplay(userType string) {
switch userType {
case "test":
<span>{ "Test user" }</span>
case "admin":
<span>{ "Admin user" }</span>
default:
<span>{ "Unknown user" }</span>
}
}
main.go
package main

import (
"context"
"os"
)

func main() {
userTypeDisplay("Other").Render(context.Background(), os.Stdout)
}
Output
<span>
Unknown user
</span>