Skip to main content

Web frameworks

Templ is framework agnostic but that does not mean it can not be used with Go frameworks and other tools.

Below are some examples of how to use templ with other Go libraries, frameworks and tools, and links to systems that have built-in templ support.


See an example of using with templ at:


See an example of using with templ at:


See an example of using with templ at:

Go Fiber

See an example of using with templ at:

gorilla/csrf is a HTTP middleware library that provides cross-site request forgery (CSRF) protection.

Follow the instructions at to add it to your project, by using the library as HTTP middleware.

package main

import (

func mustGenerateCSRFKey() (key []byte) {
key = make([]byte, 32)
n, err := rand.Read(key)
if err != nil {
if n != 32 {
panic("unable to read 32 bytes for CSRF key")

func main() {
r := http.NewServeMux()
r.Handle("/", templ.Handler(Form()))

csrfMiddleware := csrf.Protect(mustGenerateCSRFKey())
withCSRFProtection := csrfMiddleware(r)

fmt.Println("Listening on localhost:8000")
http.ListenAndServe("localhost:8000", withCSRFProtection)

Creating a CSRF templ component makes it easy to include the CSRF token in your forms.

templ Form() {
<h1>CSRF Example</h1>
<form method="post" action="/">
If you inspect the HTML form, you will see a hidden field with the value: { ctx.Value("gorilla.csrf.Token").(string) }
<input type="submit" value="Submit with CSRF token"/>
<form method="post" action="/">
You can also submit the form without the CSRF token to validate that the CSRF protection is working.
<input type="submit" value="Submit without CSRF token"/>

templ CSRF() {
<input type="hidden" name="gorilla.csrf.Token" value={ ctx.Value("gorilla.csrf.Token").(string) }/>

Project scaffolding

Other templates


See Using with Go templates