Skip to main content


templ Components are markup and code that is compiled into functions that return a templ.Component interface by running the templ generate command.

Components can contain templ elements that render HTML, text, expressions that output text or include other templates, and branching statements such as if and switch, and for loops.

package main

templ headerTemplate(name string) {
<header data-testid="headerTemplate">
<h1>{ name }</h1>

Since templ produces Go code, you can share templates the same way that you share Go code - by sharing your Go module.

templ follows the same rules as Go. If a templ block starts with an uppercase letter, then it is public, otherwise, it is private.

Code-only components

Since templ Components ultimately implement the templ.Component, any code that implements the interface can be used in place of a templ component generated from a *.templ file.

package main

import (


func button(text string) templ.Component {
return templ.ComponentFunc(func(ctx context.Context, w io.Writer) error {
_, err := io.WriteString(w, "<button>"+text+"</button>")
return err

func main() {
button("Click me").Render(context.Background(), os.Stdout)
Click me

This code is unsafe! In code-only components, you're responsible for escaping the HTML content yourself, e.g. with the templ.EscapeString function.

Method components

templ components can be returned from methods (functions attached to types).

Go code:

package main

type Data struct {
message string

templ (d Data) Method() {
<div>{ d.message }</div>

func main() {
d := Data{
message: "You can implement methods on a type.",
d.Method().Render(context.Background(), os.Stdout)