
Templ Has a Free API That Builds Type-Safe HTML Templates in Go
Templ generates Go code from HTML templates. Type-safe, fast, with LSP support in your editor. No more text/template runtime errors. Quick Start go install github.com/a-h/templ/cmd/templ@latest Define a Template // hello.templ package main templ Hello ( name string ) { < div > < h1 > Hello , { name } !</ h1 > < p > Welcome to Templ .</ p > </ div > } templ Page ( title string , body templ . Component ) { <! DOCTYPE html > < html > < head >< title > { title } </ title ></ head > < body > @ body </ body > </ html > } Use in Go func main () { http . HandleFunc ( "/" , func ( w http . ResponseWriter , r * http . Request ) { component := Page ( "Home" , Hello ( "World" )) component . Render ( r . Context (), w ) }) http . ListenAndServe ( ":3000" , nil ) } Type Safety // This won't compile — wrong type templ UserCard ( user User ) { < div > { user . Name } </ div > < div > { user . Age } </ div > // compile error if Age is int, not string } Templ vs text/template vs html/template Feature Te
Continue reading on Dev.to Webdev
Opens in a new tab

