Template
By default Fiber comes with the default HTML template engine, but this middleware contains third party rendering engines.
Installation
- go get -u github.com/gofiber/template
Signature
- template.Engine() func(raw string, bind interface{}) (out string, err error)
Template Engines
Keyword | Engine |
---|---|
Amber() | github.com/eknkc/amber |
Handlebars() | github.com/aymerick/raymond |
Mustache() | github.com/cbroglie/mustache |
Pug() | github.com/Joker/jade |
Example
- package main
- import (
- "github.com/gofiber/fiber"
- "github.com/gofiber/template"
- )
- func main() {
- app := fiber.New()
- app.Settings.TemplateEngine = template.Mustache()
- // app.Settings.TemplateEngine = template.Amber()
- // app.Settings.TemplateEngine = template.Handlebars()
- // app.Settings.TemplateEngine = template.Pug()
- app.Get("/", func(c *fiber.Ctx) {
- bind := fiber.Map{
- "name": "John",
- "age": 35,
- }
- if err := c.Render("./views/index.mustache", bind); err != nil {
- c.Status(500).Send(err.Error())
- }
- // <html><head><title>Template Demo</title></head>
- // <body>Hi, my name is John and im 35 years old
- // </body></html>
- })
- app.Listen(3000)
- }