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

    The works out-of-the-box with an optional template.Optionsopen in new window.

    • 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. Name string
    11. Author string
    12. }
    13. f.Get("/", func(t template.Template, data template.Data) {
    14. data["Name"] = "Joe"
    15. data["Books"] = []*Book{
    16. {
    17. Name: "Designing Data-Intensive Applications",
    18. Author: "Martin Kleppmann",
    19. },
    20. {
    21. Name: "Shape Up",
    22. Author: "Basecamp",
    23. },
    24. }
    25. t.HTML(http.StatusOK, "home")
    26. })
    27. f.Run()

    Using the

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

    • 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 templates
    2. import "embed"
    3. // Append "**/*" if you also have template files in subdirectories
    4. //go:embed *.tmpl
    5. var Templates embed.FS

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