Skip to content

Commit f8df2bb

Browse files
Add docs from gofiber/fiber@0fbb447
1 parent 0e79af7 commit f8df2bb

File tree

1 file changed

+192
-29
lines changed

1 file changed

+192
-29
lines changed

docs/core/guide/templates.md

Lines changed: 192 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,58 +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
21-
Render(out io.Writer, name string, binding any, layout ...string) error
55+
56+
// Outputs a template to the provided buffer using the provided template,
57+
// template name, and binded data
58+
Render(io.Writer, string, interface{}, ...string) error
2259
}
2360
```
2461

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+
2593
</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+
26108
</Tabs>
27109

28-
`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">
29130

30131
```go
31-
// 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
32139
app := fiber.New(fiber.Config{
33140
Views: engine,
34-
// Views Layout is the global layout for all template render until override on Render function.
35-
ViewsLayout: "layouts/main"
141+
})
142+
143+
app.Get("/", func (c fiber.Ctx) error {
144+
return c.Render("index", fiber.Map{
145+
"Content": "hello, World!"
146+
})
36147
})
37148
```
38149

39-
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.
40-
If the Fiber config option `PassLocalsToViews` is enabled, then all locals set using `ctx.Locals(key, value)` will be passed to the template.
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
172+
```
173+
174+
<Tabs>
175+
<TabItem value="add-func-map-example" label="AddFuncMap Example">
41176

42177
```go
43-
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 {
44192
return c.Render("index", fiber.Map{
45-
"hello": "world",
46-
});
193+
"Content": "hello, world!"
194+
})
47195
})
48196
```
49197

50-
## Engines
198+
</TabItem>
199+
<TabItem value="add-func-map-template" label="views/index.html">
200+
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+
```
210+
211+
</TabItem>
212+
</Tabs>
51213

52-
Fiber team maintains [templates](https://docs.gofiber.io/template) package that provides wrappers for multiple template engines:
214+
- For more advanced template documentation, please visit the [gofiber/template GitHub Repository](https://github.com/gofiber/template).
53215

54-
* [ace](https://docs.gofiber.io/template/ace/)
55-
* [amber](https://docs.gofiber.io/template/amber/)
56-
* [django](https://docs.gofiber.io/template/django/)
57-
* [handlebars](https://docs.gofiber.io/template/handlebars)
58-
* [html](https://docs.gofiber.io/template/html)
59-
* [jet](https://docs.gofiber.io/template/jet)
60-
* [mustache](https://docs.gofiber.io/template/mustache)
61-
* [pug](https://docs.gofiber.io/template/pug)
62-
* [slim](https://docs.gofiber.io/template/slim)
216+
## Full Example
63217

64218
<Tabs>
65219
<TabItem value="example" label="Example">
@@ -76,7 +230,8 @@ import (
76230
func main() {
77231
// Initialize standard Go html template engine
78232
engine := html.New("./views", ".html")
79-
// If you want other engine, just replace with following
233+
// If you want to use another engine,
234+
// just replace with following:
80235
// Create a new engine with django
81236
// engine := django.New("./views", ".django")
82237

@@ -86,8 +241,10 @@ func main() {
86241
app.Get("/", func(c fiber.Ctx) error {
87242
// Render index template
88243
return c.Render("index", fiber.Map{
89-
"Title": "Hello, World!",
90-
})
244+
"Title": "Go Fiber Template Example",
245+
"Description": "An example template",
246+
"Greeting": "Hello, World!",
247+
});
91248
})
92249

93250
log.Fatal(app.Listen(":3000"))
@@ -97,10 +254,16 @@ func main() {
97254
</TabItem>
98255
<TabItem value="index" label="views/index.html">
99256

100-
```markup
257+
```html
101258
<!DOCTYPE html>
259+
<html>
260+
<head>
261+
<title>{{.Title}}</title>
262+
<meta name="description" content="{{.Description}}">
263+
</head>
102264
<body>
103265
<h1>{{.Title}}</h1>
266+
<p>{{.Greeting}}</p>
104267
</body>
105268
</html>
106269
```

0 commit comments

Comments
 (0)