template

The template middleware provides HTML rendering using Go templatetemplate - 图1open in new window for Flame instances.

You can read source code of this middleware on GitHubtemplate - 图2open in new window and API documentation on pkg.go.devtemplate - 图3open in new window.

Installation

The minimum requirement of Go is 1.16.

  1. go get github.com/flamego/template

Usage examples

TIP

Examples included in this section is to demonstrate the usage of the template middleware, please refer to the documentation of html/templatetemplate - 图4open in new window package for syntax and constraints.

The template.Templatertemplate - 图5open in new window works out-of-the-box with an optional template.Optionstemplate - 图6open in new window.

By default, the templates files should resides in the “templates” directory and has extension of either .html or .tmpl. The special data type template.Datatemplate - 图7open in new window is provided as container to store any data you would want to be used in rendering the template.

Using local files

  • main.go
  • templates/home.tmpl
  1. package main
  2. import (
  3. "net/http"
  4. "github.com/flamego/flamego"
  5. "github.com/flamego/template"
  6. )
  7. func main() {
  8. f := flamego.Classic()
  9. f.Use(template.Templater())
  10. type Book struct {
  11. Name string
  12. Author string
  13. }
  14. f.Get("/", func(t template.Template, data template.Data) {
  15. data["Name"] = "Joe"
  16. data["Books"] = []*Book{
  17. {
  18. Name: "Designing Data-Intensive Applications",
  19. Author: "Martin Kleppmann",
  20. },
  21. {
  22. Name: "Shape Up",
  23. Author: "Basecamp",
  24. },
  25. }
  26. t.HTML(http.StatusOK, "home")
  27. })
  28. f.Run()
  29. }
  1. <p>
  2. Hello, <b>{{.Name}}</b>!
  3. </p>
  4. <p>
  5. Books to read:
  6. <ul>
  7. {{range .Books}}
  8. <li><i>{{.Name}}</i> by {{.Author}}</li>
  9. {{end}}
  10. </ul>
  11. </p>

Using the embed.FS

The template.EmbedFStemplate - 图8open in new window is a handy helper to convert your embed.FS into the template.FileSystemtemplate - 图9open in new window.

  • Directory
  • main.go
  • embed.go
  • home.tmpl
  1. $ tree .
  2. .
  3. ├── templates
  4. ├── embed.go
  5. ├── home.tmpl
  6. ├── go.mod
  7. ├── go.sum
  8. └── main.go
  1. package main
  2. import (
  3. "net/http"
  4. "github.com/flamego/flamego"
  5. "github.com/flamego/template"
  6. "main/templates"
  7. )
  8. func main() {
  9. f := flamego.Classic()
  10. fs, err := template.EmbedFS(templates.Templates, ".", []string{".tmpl"})
  11. if err != nil {
  12. panic(err)
  13. }
  14. f.Use(template.Templater(
  15. template.Options{
  16. FileSystem: fs,
  17. },
  18. ))
  19. type Book struct {
  20. Name string
  21. Author string
  22. }
  23. f.Get("/", func(t template.Template, data template.Data) {
  24. data["Name"] = "Joe"
  25. data["Books"] = []*Book{
  26. {
  27. Name: "Designing Data-Intensive Applications",
  28. Author: "Martin Kleppmann",
  29. },
  30. {
  31. Name: "Shape Up",
  32. Author: "Basecamp",
  33. },
  34. }
  35. t.HTML(http.StatusOK, "home")
  36. })
  37. f.Run()
  38. }
  1. package templates
  2. import "embed"
  3. // Append "**/*" if you also have template files in subdirectories
  4. //go:embed *.tmpl
  5. var Templates embed.FS
  1. <p>
  2. Hello, <b>{{.Name}}</b>!
  3. </p>
  4. <p>
  5. Books to read:
  6. <ul>
  7. {{range .Books}}
  8. <li><i>{{.Name}}</i> by {{.Author}}</li>
  9. {{end}}
  10. </ul>
  11. </p>

Template caching

When your application is running with flamego.EnvTypeDev (default) or flamego.EnvTypeTest, template files are reloaded and recomplied upon every client request.

Set the Env to flamego.EnvTypeProd to enable template caching in production.