👋 欢迎
此软件包提供了使用多个模板引擎的通用方法,可与 Fiber Web 框架 和 > v1.11.1 中可用的新 Views 接口结合使用。特别感谢 @bdtomlin 和 @arsmn 的帮助!
支持 9 种模板引擎
安装
需要 Go 版本
1.17或更高版本。
go get -u github.com/gofiber/fiber/v2
go get -u github.com/gofiber/template/any_template_engine/vX
示例
package main
import (
"log"
"github.com/gofiber/fiber/v2"
// To use a specific template engine, import as shown below:
// "github.com/gofiber/template/pug"
// "github.com/gofiber/template/mustache"
// etc..
// In this example we use the html template engine
"github.com/gofiber/template/html/v2"
)
func main() {
// Create a new engine by passing the template folder
// and template extension using <engine>.New(dir, ext string)
engine := html.New("./views", ".html")
// We also support the http.FileSystem interface
// See examples below to load templates from embedded files
engine := html.NewFileSystem(http.Dir("./views"), ".html")
// Reload the templates on each render, good for development
engine.Reload(true) // Optional. Default: false
// Debug will print each template that is parsed, good for debugging
engine.Debug(true) // Optional. Default: false
// Layout defines the variable name that is used to yield templates within layouts
engine.Layout("embed") // Optional. Default: "embed"
// Delims sets the action delimiters to the specified strings
engine.Delims("{{", "}}") // Optional. Default: engine delimiters
// AddFunc adds a function to the template's global function map.
engine.AddFunc("greet", func(name string) string {
return "Hello, " + name + "!"
})
// After you created your engine, you can pass it to Fiber's Views Engine
app := fiber.New(fiber.Config{
Views: engine,
})
// To render a template, you can call the ctx.Render function
// Render(tmpl string, values interface{}, layout ...string)
app.Get("/", func(c *fiber.Ctx) error {
return c.Render("index", fiber.Map{
"Title": "Hello, World!",
})
})
// Render with layout example
app.Get("/layout", func(c *fiber.Ctx) error {
return c.Render("index", fiber.Map{
"Title": "Hello, World!",
}, "layouts/main")
})
log.Fatal(app.Listen(":3000"))
}
更多示例
要查看更多特定示例,您可以访问每个引擎文件夹以了解更多信息
嵌入式系统
我们支持 http.FileSystem 接口,因此您可以使用不同的库从嵌入式二进制文件中加载模板。
pkger
阅读文档: https://github.com/markbates/pkger
package main
import (
"log"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html"
"github.com/markbates/pkger"
)
func main() {
engine := html.NewFileSystem(pkger.Dir("/views"), ".html")
app := fiber.New(fiber.Config{
Views: engine,
})
// run pkger && go build
}
packr
阅读文档: https://github.com/gobuffalo/packr
package main
import (
"log"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html"
"github.com/gobuffalo/packr/v2"
)
func main() {
engine := html.NewFileSystem(packr.New("Templates", "/views"), ".html")
app := fiber.New(fiber.Config{
Views: engine,
})
// run packr && go build
}
go.rice
阅读文档: https://github.com/GeertJohan/go.rice
package main
import (
"log"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html"
"github.com/GeertJohan/go.rice"
)
func main() {
engine := html.NewFileSystem(rice.MustFindBox("views").HTTPBox(), ".html")
app := fiber.New(fiber.Config{
Views: engine,
})
// run rice embed-go && go build
}
fileb0x
阅读文档: https://github.com/UnnoTed/fileb0x
package main
import (
"log"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html"
// your generated package
"github.com/<user>/<repo>/static"
)
func main() {
engine := html.NewFileSystem(static.HTTP, ".html")
app := fiber.New(fiber.Config{
Views: engine,
})
// Read the documentation on how to use fileb0x
}
基准测试
简单

扩展

基准测试在 Apple Macbook M1 上运行。每个引擎经过 20 次基准测试,结果平均到一个 xlsx 文件中。Mustache 已从扩展基准测试中排除