template

The template middleware provides HTML rendering using Go templateopen in new window for Flame instances.

You can read source code of this middleware on GitHubopen in new window and API documentation on pkg.go.devopen in new window.

Installation

The minimum requirement of Go is 1.16.

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/templateopen in new window package for syntax and constraints.

The template.Templateropen in new window works out-of-the-box with an optional template.Optionsopen 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.Dataopen in new window is provided as container to store any data you would want to be used in rendering the template.

Using local files

package main

import (
	"net/http"

	"github.com/flamego/flamego"
	"github.com/flamego/template"
)

func main() {
	f := flamego.Classic()
	f.Use(template.Templater())

	type Book struct {
		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()
}
<p>
  Hello, <b>{{.Name}}</b>!
</p>
<p>
  Books to read:
  <ul>
    {{range .Books}}
      <li><i>{{.Name}}</i> by {{.Author}}</li>
    {{end}}
  </ul>
</p>

Using the embed.FS

The template.EmbedFSopen in new window is a handy helper to convert your embed.FS into the template.FileSystemopen in new window.

$ tree .
.
├── templates
│   ├── embed.go
│   ├── home.tmpl
├── go.mod
├── go.sum
└── main.go
package main

import (
	"net/http"

	"github.com/flamego/flamego"
	"github.com/flamego/template"

	"main/templates"
)

func main() {
	f := flamego.Classic()

	fs, err := template.EmbedFS(templates.Templates, ".", []string{".tmpl"})
	if err != nil {
		panic(err)
	}
	f.Use(template.Templater(
		template.Options{
			FileSystem: fs,
		},
	))

	type Book struct {
		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()
}
package templates

import "embed"

// Append "**/*" if you also have template files in subdirectories
//go:embed *.tmpl
var Templates embed.FS
<p>
  Hello, <b>{{.Name}}</b>!
</p>
<p>
  Books to read:
  <ul>
    {{range .Books}}
      <li><i>{{.Name}}</i> by {{.Author}}</li>
    {{end}}
  </ul>
</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.