template
You can read source code of this middleware on GitHubopen in new window and API documentation on .
The minimum requirement of Go is 1.16.
Examples included in this section is to demonstrate the usage of the template middleware, please refer to the documentation of html/template
open in new window package for syntax and constraints.
The works out-of-the-box with an optional template.Options
open in new window.
- main.go
- templates/home.tmpl
package main
import (
"net/http"
"github.com/flamego/flamego"
"github.com/flamego/template"
)
func main() {
f := flamego.Classic()
f.Use(template.Templater())
Name string
Author string
}
f.Get("/", func(t template.Template, data template.Data) {
data["Name"] = "Joe"
data["Books"] = []*Book{
{
Name: "Designing Data-Intensive Applications",
Author: "Martin Kleppmann",
},
{
Name: "Shape Up",
Author: "Basecamp",
},
}
t.HTML(http.StatusOK, "home")
})
f.Run()
Using the
The template.EmbedFS
open in new window is a handy helper to convert your embed.FS
into the .
- Directory
- main.go
- embed.go
- home.tmpl
$ tree .
.
├── templates
│ ├── embed.go
│ ├── home.tmpl
├── go.mod
├── go.sum
└── main.go
package templates
import "embed"
// Append "**/*" if you also have template files in subdirectories
//go:embed *.tmpl
var Templates embed.FS
When your application is running with flamego.EnvTypeDev
(default) or , template files are reloaded and recomplied upon every client request.