Documentation
Iris supports 8 template engines out-of-the-box, developers can still use any external golang template engine, as Context.ResponseWriter() is an io.Writer.
All template engines share a common API i.e. Parse using embedded assets, Layouts and Party-specific layout, Template Funcs, Partial Render and more.
#
Name
Parser
1
HTML
2
Blocks
3
Django
4
Pug
5
Handlebars
6
Amber
7
Jet
8
Ace
A view engine can be registered per-Party. To register a view engine use the Application/Party.RegisterView(ViewEngine) method as shown below.
Load all templates from the "./views" folder where extension is ".html" and parse them using the standard html/template package.
1
// [app := iris.New...]
2
tmpl := iris.HTML("./views", ".html")
3
app.RegisterView(tmpl)
Copied!

Render

To render or execute a view use the Context.View method inside the main route's handler. E.g. to load the ./views/hi.html template file:
1
ctx.View("hi") // or hi.html
Copied!

Pass Data

To bind Go values with key-value pattern inside a view through middleware or main handler use the Context.ViewData method before the Context.View one.
Bind: {{.message}} with "Hello world!".
1
ctx.ViewData("message", "Hello world!")
Copied!
To bind a Go model to a view you have two options:
1.
1
ctx.ViewData("user", User{})
2
3
// variable binding as {{.user.Name}}
Copied!
2.
1
ctx.View("user-page.html", User{})
2
3
// root binding as {{.Name}}
Copied!
To add a template function use the AddFunc method of the preferred view engine.
1
// func name, input arguments, render value
2
tmpl.AddFunc("greet", func(s string) string {
3
return "Greetings " + s + "!"
4
})
Copied!
To reload on each request set the view engine's Reload method.
1
tmpl.Reload(true)
Copied!

Embedded

To use embedded templates and not depend on local file system use the go-bindata external tool and pass its Asset and AssetNames functions to the Binary method of the preferred view engine.
1
tmpl.Binary(Asset, AssetNames)
Copied!
Example Code:
Please read the comments too.
1
// file: main.go
2
package main
3
4
import "github.com/kataras/iris/v12"
5
6
func main() {
7
app := iris.New()
8
9
// Parse all templates from the "./views" folder
10
// where extension is ".html" and parse them
11
// using the standard `html/template` package.
12
tmpl := iris.HTML("./views", ".html")
13
14
// Enable re-build on local template files changes.
15
tmpl.Reload(true)
16
17
// Default template funcs are:
18
//
19
// - {{ urlpath "myNamedRoute" "pathParameter_ifNeeded" }}
20
// - {{ render "header.html" }}
21
// and partial relative path to current page:
22
// - {{ render_r "header.html" }}
23
// - {{ yield }}
24
// - {{ current }}
25
// Register a custom template func:
26
tmpl.AddFunc("greet", func(s string) string {
27
return "Greetings " + s + "!"
28
})
29
30
// Register the view engine to the views,
31
// this will load the templates.
32
app.RegisterView(tmpl)
33
34
// Method: GET
35
// Resource: http://localhost:8080
36
app.Get("/", func(ctx iris.Context) {
37
// Bind: {{.message}} with "Hello world!"
38
ctx.ViewData("message", "Hello world!")
39
// Render template file: ./views/hi.html
40
ctx.View("hi.html")
41
})
42
43
app.Listen(":8080")
44
}
Copied!
1
<!-- file: ./views/hi.html -->
2
<html>
3
<head>
4
<title>Hi Page</title>
5
</head>
6
<body>
7
<h1>{{.message}}</h1>
8
<strong>{{greet "to you"}}</strong>
9
</body>
10
</html>
Copied!
Open a browser tab at http://localhost:8080.
The rendered result will look like this:
1
<html>
2
<head>
3
<title>Hi Page</title>
4
</head>
5
<body>
6
<h1>Hello world!</h1>
7
<strong>Greetings to you!</strong>
8
</body>
9
</html>
Copied!

Multitemplate

Iris allows unlimited number of registered view engines per Application. Besides that, you can register a view engine per Party or through middleware too!.
1
// Register a view engine per group of routes.
2
adminGroup := app.Party("/admin")
3
adminGroup.RegisterView(iris.Blocks("./views/admin", ".html"))
Copied!

Through Middleware

1
func middleware(views iris.ViewEngine) iris.Handler {
2
return func(ctx iris.Context) {
3
ctx.ViewEngine(views)
4
ctx.Next()
5
}
6
}
Copied!
Usage
1
// Register a view engine on-fly for the current chain of handlers.
2
views := iris.Blocks("./views/on-fly", ".html")
3
views.Load()
4
5
app.Get("/", setViews(views), onFly)
Copied!
Last modified 1yr ago