Iris 通过其通用视图引擎为 6 个模板解析器提供了开箱即用的支持。 当然,开发人员仍可以使用各种go模板解析器作为 `Context.ResponseWriter()` 去完成 the `http.ResponseWriter` 和 ` io.Writer`。
Iris 提出了一些默认的解析器不支持的通用规则和功能。例如,我们支持 yield, render, render_r, current, urlpath 模板函数和 Layouts 和 跨中间件绑定和为引擎嵌入模板文件。
要使用一个模板引擎的独特功能,必须通过阅读他的文档来学习特征和语法 (单击下面的链接)。选择最适合你应用程序需求的模版引擎。
让我们看一下内置视图引擎的列表:
|引擎 |声明 |下划线模板解析器|
| -------- | -------- | -------- |
| std template/html |`iris.HTML(...)`| [html/template](https://golang.org/pkg/html/template/) 包|
django|` iris.Django(...)`| [iris-contrib/pongo2](https://github.com/iris-contrib/pongo2) 包|
|handlebars| `iris.Handlebars(...)`| [Joker/jade](https://github.com/Joker/jade) 包|
|amber| `iris.Amber(...)` |[aymerick/raymond](https://github.com/aymerick/raymond) 包|
|pug(jade) |`iris.Pug(...)` |[eknkc/amber](https://github.com/eknkc/amber) 包|
|jet| `iris.Jet(...)` |[CloudyKit/jet](https://github.com/CloudyKit/jet) 包|
一个或多个视图引擎可以在同一应用程序中注册。要 **注册** 一个视图引擎,需要使用 `RegisterView(ViewEngine)` 方法。
从 "./views" 文件夹加载所有扩展名为".html" 的模版。 并且使用` html/template` 标准包解析它们。
```
// [app := iris.New...]
tmpl := iris.HTML("./views", ".html")
app.RegisterView(tmpl)
```
使用 `Context.View` 方法在主程序的路由的处理程序,**渲染** 或者 **执行** 一个视图。
```
ctx.View("hi.html")
```
通过键值模式在一个视图里通过中间件或者主要处理程序在 `Context.View` 方法之前,使用 `Context.ViewData` 方法 **绑定** Go 的值。
绑定: {{.message}} 和 "Hello world!"。
```
ctx.ViewData("message", "Hello world!")
```
要将Go模型绑定到视图,你有两个选择:
* `ctx.ViewData("user", User{}) `- 例如变量绑定为 `{{.user.Name}} `
* `ctx.View("user-page.html", User{})` - 例如 root 变量绑定为` {{.Name}}`
使用选视图引擎的 `AddFunc` 方法,**添加模板功能**。
```
// 函数名称,输入参数,渲染值
tmpl.AddFunc("greet", func(s string) string {
return "Greetings " + s + "!"
})
```
调用视图引擎的 `Reload` 方法,用来实现 **在本地文件更改上重新加载** 。
```
tmpl.Reload(true)
```
使用 **嵌入式** 模版和而不依赖于本地文件系统, 使用[go-bindata](https://github.com/go-bindata/go-bindata)外部工具并传递它的 `Asset` 和 `AssetNames` 方法首选视图引擎的 `Binary`方法起作用。
```
tmpl.Binary(Asset, AssetNames)
```
示例代码:
请认真阅读注释和代码。
```
// 文件: main.go
package main
import "github.com/kataras/iris/v12"
func main() {
app := iris.New()
// 解析 "./views" 文件夹中的所有模板
// 扩展名为 ".html" 并解析它们
// 使用标准的 `html/template` 包。
tmpl := iris.HTML("./views", ".html")
// 启用对本地模板文件更改的重新构建。
tmpl.Reload(true)
// 默认模板功能为:
//
// - {{ urlpath "myNamedRoute" "pathParameter_ifNeeded" }}
// - {{ render "header.html" }}
// 和当前页面的部分相对路径:
// - {{ render_r "header.html" }}
// - {{ yield }}
// - {{ current }}
// 注册自定义模板功能:
tmpl.AddFunc("greet", func(s string) string {
return "Greetings " + s + "!"
})
// 将视图引擎注册到视图,
// 这将加载模板。
app.RegisterView(tmpl)
// 方法: GET
// 资源: http://localhost:8080
app.Get("/", func(ctx iris.Context) {
// 绑定: {{.message}} 与 "Hello world!"
ctx.ViewData("message", "Hello world!")
// 渲染模板文件: ./views/hi.html
ctx.View("hi.html")
})
app.Run(iris.Addr(":8080"))
}
```
```
<!-- file: ./views/hi.html -->
<html>
<head>
<title>Hi Page</title>
</head>
<body>
<h1>{{.message}}</h1>
<strong>{{greet "to you"}}</strong>
</body>
</html>
```
打开浏览器选项卡,输入 http://localhost:8080 。
呈现的结果将如下所示:
```
<html>
<head>
<title>Hi Page</title>
</head>
<body>
<h1>Hello world!</h1>
<strong>Greetings to you!</strong>
</body>
</html>
```