Template

By default Fiber comes with the default HTML template engine, but this middleware contains third party rendering engines.

Installation

  1. go get -u github.com/gofiber/template

Signature

  1. template.Engine() func(raw string, bind interface{}) (out string, err error)

Template Engines

KeywordEngine
Amber()github.com/eknkc/amber
Handlebars()github.com/aymerick/raymond
Mustache()github.com/cbroglie/mustache
Pug()github.com/Joker/jade

Example

  1. package main
  2.  
  3. import (
  4. "github.com/gofiber/fiber"
  5. "github.com/gofiber/template"
  6. )
  7.  
  8. func main() {
  9. app := fiber.New()
  10.  
  11. app.Settings.TemplateEngine = template.Mustache()
  12. // app.Settings.TemplateEngine = template.Amber()
  13. // app.Settings.TemplateEngine = template.Handlebars()
  14. // app.Settings.TemplateEngine = template.Pug()
  15.  
  16. app.Get("/", func(c *fiber.Ctx) {
  17. bind := fiber.Map{
  18. "name": "John",
  19. "age": 35,
  20. }
  21. if err := c.Render("./views/index.mustache", bind); err != nil {
  22. c.Status(500).Send(err.Error())
  23. }
  24. // <html><head><title>Template Demo</title></head>
  25. // <body>Hi, my name is John and im 35 years old
  26. // </body></html>
  27. })
  28.  
  29. app.Listen(3000)
  30. }