跳到主要内容
版本: v1.8.3_v1.x.x

👋 欢迎

FiberFiber

此包提供通用方法,通过使用 > v1.11.1 版本中新的 Views 接口,将多种模板引擎与 Fiber web 框架 一起使用。特别感谢 @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 被排除在扩展基准测试之外。