-
Notifications
You must be signed in to change notification settings - Fork 0
Templ
Templ is an open-source, code-generation-based, type-safe HTML templating framework for Go. Its main goal is to eliminate the risk of runtime template errors by making use of Go’s compile-time checks and to provide a convenient DSL (Domain Specific Language) for writing dynamic user interfaces. 1 2
Templ was created by Andrew Hood (GitHub: @a-h) around 2019–2020 1, 3. He sought a templating solution that was both idiomatic to Go and type-safe, circumventing the runtime-only checks commonly found in text/template
or html/template
.
Templ emerged as a side project and evolved through community contributions. Early commits date back to November 2019 4. It’s licensed under the MIT license 1.
-
Type-Safe Templating
-
Code Generation
-
Modular Architecture
-
Integration
-
Open Source & Community-Driven
-
Write Templ DSL:
A.templ
file uses HTML-like syntax with embedded Go expressions:Template MyTemplate(person Person) { div { h1("Hello, " + person.Name) if person.Age > 18 { p("You are an adult.") } else { p("You are a minor.") } } }
-
Run Templ CLI:
templ generate
This generates
.go
files with strongly typed functions 2, 5. -
Import & Render:
func handler(w http.ResponseWriter, r *http.Request) { myTemplate := MyTemplate(Person{Name: "Alice", Age: 25}) w.Write([]byte(myTemplate)) }
The rendered output is pure HTML 5.
-
Compile-Time Safety
- Prevents runtime template surprises via immediate feedback during compilation 1.
-
Performance
-
Developer Experience
-
Flexible Integration
-
Lightweight
- Everything compiles into your Go binary with no additional templating engines or runtime interpreters 1.
-
Learning Curve
-
Limited Ecosystem
-
Increased Build Complexity
- A code generation phase means CI/CD pipelines must run
templ generate
before compiling 2.
- A code generation phase means CI/CD pipelines must run
-
Server-Side Web Rendering: A type-safe alternative to
html/template
for building server-rendered UIs 1, [5]. - Email Template Generation: Avoid runtime issues when generating HTML email content 1, 2.
- Microservices / Small UIs: Ideal for mini web interfaces in microservices 2.
- Reusable UI Components: Build partials and layouts that can be composed into larger pages 5.
go install github.com/a-h/templ/cmd/templ@latest
Installs the Templ CLI tool into $GOPATH/bin
2.
myproject/
├── main.go
└── templates/
└── index.templ
A common folder layout for Templ-based projects 2, 5.
Template Index(name string) {
html {
head {
title("Welcome!")
}
body {
h1("Hello " + name)
}
}
}
A basic example that uses Templ’s DSL 2, 5.
templ generate
go build .
Templ CLI creates index_templ.go
that you can compile alongside your Go code 2.
package main
import (
"log"
"net/http"
"myproject/templates" // The generated file
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
output := templates.Index("Alice")
w.Write([]byte(output))
})
log.Fatal(http.ListenAndServe(":8080", nil))
}
A standard approach using Templ in a Go application 2, 5.
-
Official GitHub Repository
https://github.com/a-h/templ
Read the README and explore the code.Source: 1
-
Templ Wiki / Docs
Templ Wiki
Contains official guides, advanced usage, and examples.Source: 2
-
Example Projects
Examples Folder
Real-world code samples.Source: 5
-
Community Articles / Tutorials
- Search Medium or dev.to for Templ tutorials.
- Community blog posts often linked in Discussions.
Source: 3
-
Community Screencasts
Check the “Discussions” section or pinned issues on GitHub for user-created video tutorials or demos.
(Templ doesn’t yet have official YouTube presence like Go does, but user-made content is emerging.)
-
Templ GitHub Repository - README
- Official README with an introduction, high-level overview, and basic instructions.
-
Templ Wiki
- Official documentation covering usage patterns, “getting started,” advanced configuration, partials, layouts, etc.
-
Templ GitHub Discussions
- Community Q&A, announcements, and knowledge sharing.
-
Templ Initial Commits History
- Shows some of the earliest code, indicating the timeline (circa late 2019).
-
Templ Examples Folder
- Contains actual code samples for reference, from simple “Hello World” to more complex partial/layout usage.