Skip to content

Commit 0e79af7

Browse files
committed
add new template guide to v2
1 parent 29a220e commit 0e79af7

File tree

1 file changed

+196
-31
lines changed

1 file changed

+196
-31
lines changed

versioned_docs/version-v2.x/guide/templates.md

Lines changed: 196 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,57 +8,212 @@ sidebar_position: 3
88
import Tabs from '@theme/Tabs';
99
import TabItem from '@theme/TabItem';
1010

11-
## Template interfaces
11+
Templates are a great tool to render dynamic content without using a separate frontend framework.
1212

13-
Fiber provides a Views interface to provide your own template engine:
13+
## Template Engines
1414

15-
<Tabs>
16-
<TabItem value="views" label="Views">
15+
Fiber allows you to provide a custom template engine at app initialization.
1716

1817
```go
18+
app := fiber.New(fiber.Config{
19+
// Pass in Views Template Engine
20+
Views: engine,
21+
22+
// Default global path to search for views (can be overriden when calling Render())
23+
ViewsLayout: "layouts/main",
24+
25+
// Enables/Disables access to `ctx.Locals()` entries in rendered views
26+
// (defaults to false)
27+
PassLocalsToViews: false,
28+
})
29+
```
30+
31+
32+
### Supported Engines
33+
34+
The Fiber team maintains a [templates](https://docs.gofiber.io/template) package that provides wrappers for multiple template engines:
35+
36+
* [ace](https://docs.gofiber.io/template/ace/)
37+
* [amber](https://docs.gofiber.io/template/amber/)
38+
* [django](https://docs.gofiber.io/template/django/)
39+
* [handlebars](https://docs.gofiber.io/template/handlebars)
40+
* [html](https://docs.gofiber.io/template/html)
41+
* [jet](https://docs.gofiber.io/template/jet)
42+
* [mustache](https://docs.gofiber.io/template/mustache)
43+
* [pug](https://docs.gofiber.io/template/pug)
44+
* [slim](https://docs.gofiber.io/template/slim)
45+
46+
:::info
47+
Custom template engines can implement the `Views` interface to be supported in Fiber.
48+
:::
49+
50+
51+
```go title="Views interface"
1952
type Views interface {
53+
// Fiber executes Load() on app initialization to load/parse the templates
2054
Load() error
55+
56+
// Outputs a template to the provided buffer using the provided template,
57+
// template name, and binded data
2158
Render(io.Writer, string, interface{}, ...string) error
2259
}
2360
```
61+
62+
:::note
63+
The `Render` method is linked to the [**ctx.Render\(\)**](../api/ctx.md#render) function that accepts a template name and binding data.
64+
:::
65+
66+
67+
## Rendering Templates
68+
69+
Once an engine is set up, a route handler can call the [**ctx.Render\(\)**](../api/ctx.md#render) function with a template name and binded data to send the rendered template.
70+
71+
```go title="Signature"
72+
func (c *Ctx) Render(name string, bind Map, layouts ...string) error
73+
```
74+
75+
:::info
76+
By default, [**ctx.Render\(\)**](../api/ctx.md#render) searches for the template name in the `ViewsLayout` path. To override this setting, provide the path(s) in the `layouts` argument.
77+
:::
78+
79+
80+
<Tabs>
81+
82+
<TabItem value="example" label="Example">
83+
84+
```go
85+
app.Get("/", func(c *fiber.Ctx) error {
86+
return c.Render("index", fiber.Map{
87+
"Title": "Hello, World!",
88+
})
89+
90+
})
91+
```
92+
2493
</TabItem>
94+
95+
<TabItem value="index" label="layouts/index.html">
96+
97+
```html
98+
<!DOCTYPE html>
99+
<html>
100+
<body>
101+
<h1>{{.Title}}</h1>
102+
</body>
103+
</html>
104+
```
105+
106+
</TabItem>
107+
25108
</Tabs>
26109

27-
`Views` interface contains a `Load` and `Render` method, `Load` is executed by Fiber on app initialization to load/parse the templates.
110+
:::caution
111+
If the Fiber config option `PassLocalsToViews` is enabled, then all locals set using `ctx.Locals(key, value)` will be passed to the template. It is important to avoid clashing keys when using this setting.
112+
:::
113+
114+
## Advanced Templating
115+
116+
### Custom Functions
117+
118+
Fiber supports adding custom functions to templates.
119+
120+
#### AddFunc
121+
122+
Adds a global function to all templates.
123+
124+
```go title="Signature"
125+
func (e *Engine) AddFunc(name string, fn interface{}) IEngineCore
126+
```
127+
128+
<Tabs>
129+
<TabItem value="add-func-example" label="AddFunc Example">
28130

29131
```go
30-
// Pass engine to Fiber's Views Engine
132+
// Add `ToUpper` to engine
133+
engine := html.New("./views", ".html")
134+
engine.AddFunc("ToUpper", func(s string) string {
135+
return strings.ToUpper(s)
136+
}
137+
138+
// Initialize Fiber App
31139
app := fiber.New(fiber.Config{
32140
Views: engine,
33-
// Views Layout is the global layout for all template render until override on Render function.
34-
ViewsLayout: "layouts/main"
35141
})
142+
143+
app.Get("/", func (c *fiber.Ctx) error {
144+
return c.Render("index", fiber.Map{
145+
"Content": "hello, world!"
146+
})
147+
})
148+
```
149+
150+
</TabItem>
151+
<TabItem value="add-func-template" label="views/index.html">
152+
153+
```html
154+
<!DOCTYPE html>
155+
<html>
156+
<body>
157+
<p>This will be in {{ToUpper "all caps"}}:</p>
158+
<p>{{ToUpper .Content}}</p>
159+
</body>
160+
</html>
161+
```
162+
163+
</TabItem>
164+
</Tabs>
165+
166+
#### AddFuncMap
167+
168+
Adds a Map of functions (keyed by name) to all templates.
169+
170+
```go title="Signature"
171+
func (e *Engine) AddFuncMap(m map[string]interface{}) IEngineCore
36172
```
37173

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.
174+
<Tabs>
175+
<TabItem value="add-func-map-example" label="AddFuncMap Example">
40176

41177
```go
42-
app.Get("/", func(c *fiber.Ctx) error {
178+
// Add `ToUpper` to engine
179+
engine := html.New("./views", ".html")
180+
engine.AddFuncMap(map[string]interface{}{
181+
"ToUpper": func(s string) string {
182+
return strings.ToUpper(s)
183+
},
184+
})
185+
186+
// Initialize Fiber App
187+
app := fiber.New(fiber.Config{
188+
Views: engine,
189+
})
190+
191+
app.Get("/", func (c *fiber.Ctx) error {
43192
return c.Render("index", fiber.Map{
44-
"hello": "world",
45-
});
193+
"Content": "hello, world!"
194+
})
46195
})
47196
```
48197

49-
## Engines
198+
</TabItem>
199+
<TabItem value="add-func-map-template" label="views/index.html">
50200

51-
Fiber team maintains [templates](https://docs.gofiber.io/template) package that provides wrappers for multiple template engines:
201+
```html
202+
<!DOCTYPE html>
203+
<html>
204+
<body>
205+
<p>This will be in {{ToUpper "all caps"}}:</p>
206+
<p>{{ToUpper .Content}}</p>
207+
</body>
208+
</html>
209+
```
52210

53-
* [ace](https://docs.gofiber.io/template/ace/)
54-
* [amber](https://docs.gofiber.io/template/amber/)
55-
* [django](https://docs.gofiber.io/template/django/)
56-
* [handlebars](https://docs.gofiber.io/template/handlebars)
57-
* [html](https://docs.gofiber.io/template/html)
58-
* [jet](https://docs.gofiber.io/template/jet)
59-
* [mustache](https://docs.gofiber.io/template/mustache)
60-
* [pug](https://docs.gofiber.io/template/pug)
61-
* [slim](https://docs.gofiber.io/template/slim)
211+
</TabItem>
212+
</Tabs>
213+
214+
- For more advanced template documentation, please visit the [gofiber/template GitHub Repository](https://github.com/gofiber/template).
215+
216+
## Full Example
62217

63218
<Tabs>
64219
<TabItem value="example" label="Example">
@@ -75,7 +230,8 @@ import (
75230
func main() {
76231
// Initialize standard Go html template engine
77232
engine := html.New("./views", ".html")
78-
// If you want other engine, just replace with following
233+
// If you want to use another engine,
234+
// just replace with following:
79235
// Create a new engine with django
80236
// engine := django.New("./views", ".django")
81237

@@ -85,8 +241,10 @@ func main() {
85241
app.Get("/", func(c *fiber.Ctx) error {
86242
// Render index template
87243
return c.Render("index", fiber.Map{
88-
"Title": "Hello, World!",
89-
})
244+
"Title": "Go Fiber Template Example",
245+
"Description": "An example template",
246+
"Greeting": "Hello, world!",
247+
});
90248
})
91249

92250
log.Fatal(app.Listen(":3000"))
@@ -95,12 +253,19 @@ func main() {
95253
</TabItem>
96254
<TabItem value="index" label="views/index.html">
97255

98-
```markup
256+
```html
99257
<!DOCTYPE html>
100-
<body>
101-
<h1>{{.Title}}</h1>
102-
</body>
258+
<html>
259+
<head>
260+
<title>{{.Title}}</title>
261+
<meta name="description" content="{{.Description}}">
262+
</head>
263+
<body>
264+
<h1>{{.Title}}</h1>
265+
<p>{{.Greeting}}</p>
266+
</body>
103267
</html>
104268
```
269+
105270
</TabItem>
106271
</Tabs>

0 commit comments

Comments
 (0)