github.com/gofiber/fiber/v2@v2.47.0/docs/guide/templates.md (about) 1 --- 2 id: templates 3 title: 📝 Templates 4 description: Fiber supports server-side template engines. 5 sidebar_position: 3 6 --- 7 8 import Tabs from '@theme/Tabs'; 9 import TabItem from '@theme/TabItem'; 10 11 ## Template interfaces 12 13 Fiber provides a Views interface to provide your own template engine: 14 15 <Tabs> 16 <TabItem value="views" label="Views"> 17 18 ```go 19 type Views interface { 20 Load() error 21 Render(io.Writer, string, interface{}, ...string) error 22 } 23 ``` 24 </TabItem> 25 </Tabs> 26 27 `Views` interface contains a `Load` and `Render` method, `Load` is executed by Fiber on app initialization to load/parse the templates. 28 29 ```go 30 // Pass engine to Fiber's Views Engine 31 app := fiber.New(fiber.Config{ 32 Views: engine, 33 // Views Layout is the global layout for all template render until override on Render function. 34 ViewsLayout: "layouts/main" 35 }) 36 ``` 37 38 The `Render` method is linked to the [**ctx.Render\(\)**](../api/ctx.md#render) function that accepts a template name and binding data. It will use global layout if layout is not being defined in `Render` function. 39 If the Fiber config option `PassLocalsToViews` is enabled, then all locals set using `ctx.Locals(key, value)` will be passed to the template. 40 41 ```go 42 app.Get("/", func(c *fiber.Ctx) error { 43 return c.Render("index", fiber.Map{ 44 "hello": "world", 45 }); 46 }) 47 ``` 48 49 ## Engines 50 51 Fiber team maintains [templates](https://github.com/gofiber/template) package that provides wrappers for multiple template engines: 52 53 * [html](https://github.com/gofiber/template/tree/master/html) 54 * [ace](https://github.com/gofiber/template/tree/master/ace) 55 * [amber](https://github.com/gofiber/template/tree/master/amber) 56 * [django](https://github.com/gofiber/template/tree/master/django) 57 * [handlebars](https://github.com/gofiber/template/tree/master/handlebars) 58 * [jet](https://github.com/gofiber/template/tree/master/jet) 59 * [mustache](https://github.com/gofiber/template/tree/master/mustache) 60 * [pug](https://github.com/gofiber/template/tree/master/pug) 61 62 <Tabs> 63 <TabItem value="example" label="Example"> 64 65 ```go 66 package main 67 68 import ( 69 "log" 70 "github.com/gofiber/fiber/v2" 71 "github.com/gofiber/template/html/v2" 72 ) 73 74 func main() { 75 // Initialize standard Go html template engine 76 engine := html.New("./views", ".html") 77 // If you want other engine, just replace with following 78 // Create a new engine with django 79 // engine := django.New("./views", ".django") 80 81 app := fiber.New(fiber.Config{ 82 Views: engine, 83 }) 84 app.Get("/", func(c *fiber.Ctx) error { 85 // Render index template 86 return c.Render("index", fiber.Map{ 87 "Title": "Hello, World!", 88 }) 89 }) 90 91 log.Fatal(app.Listen(":3000")) 92 } 93 ``` 94 </TabItem> 95 <TabItem value="index" label="views/index.html"> 96 97 ```markup 98 <!DOCTYPE html> 99 <body> 100 <h1>{{.Title}}</h1> 101 </body> 102 </html> 103 ``` 104 </TabItem> 105 </Tabs>