Fiber provides a Views interface to provide your own template engine:
type Views interface {Load() errorRender(io.Writer, string, interface{}, ...string) error}
Views
interface contains a Load
and Render
method, Load
is executed by Fiber on app initialization to load/parse the templates.
// Pass engine to Fiber's Views Engineapp := fiber.New(fiber.Config{Views: engine,})
The Render
method is linked to the ctx.Render() function that accepts a template name and binding data.
app.Get("/", func(c *fiber.Ctx) error {return c.Render("index", fiber.Map{"hello": "world",});})
Fiber team maintains templates package that provides wrappers for multiple template engines:
package mainimport ("github.com/gofiber/fiber/v2""github.com/gofiber/template/html")func main() {// Initialize standard Go html template engineengine := html.New("./views", ".html")app := fiber.New(fiber.Config{Views: engine,})app.Get("/", func(c *fiber.Ctx) error {// Render index templatereturn c.Render("index", fiber.Map{"Title": "Hello, World!",})})app.Listen(":3000")}
<!DOCTYPE html><body><h1>{{.Title}}</h1></body></html>