# 视图
Iris支持 5 种开箱即用的模板引擎,开发者依然可以选择使用任何 Golang 之外的模板引擎, 像 context/context#ResponseWriter()
是一个 io.Writer
。
所有这五个模板引擎的公共 API 都具有相同的特性,像布局,模板函数,局部定制布局,部分展示等。
- 标准的 html,它的模板解析器是 golang.org/pkg/html/template/ (opens new window)
- Django,它的模板解析器是 github.com/flosch/pongo2 (opens new window)
- Pug(Jade),它的模板解析器是 github.com/Joker/jade (opens new window)
- Handlebars,它的模板解析器是 github.com/aymerick/raymond (opens new window)
- Amber,它的模板解析器是 github.com/eknkc/amber (opens new window)
# 概览
// file: main.go
package main
import "github.com/kataras/iris"
func main() {
app := iris.New()
// 从 "./views" 目录下加载扩展名是".html" 的所有模板,
// 并使用标准的 `html/template` 包进行解析。
app.RegisterView(iris.HTML("./views", ".html"))
// Method: GET
// Resource: http://localhost:8080
app.Get("/", func(ctx iris.Context) {
// 绑定: {{.message}} 为 "Hello world!"
ctx.ViewData("message", "Hello world!")
// 渲染模板文件: ./views/hello.html
ctx.View("hello.html")
})
// Method: GET
// Resource: http://localhost:8080/user/42
app.Get("/user/{id:long}", func(ctx iris.Context) {
userID, _ := ctx.Params().GetInt64("id")
ctx.Writef("User ID: %d", userID)
})
// 使用网络地址启动服务
app.Run(iris.Addr(":8080"))
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!-- 文件: ./views/hello.html -->
<html>
<head>
<title>Hello Page</title>
</head>
<body>
<h1>{{.message}}</h1>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 模板函数
package main
import "github.com/kataras/iris"
func main() {
app := iris.New()
// - standard html | iris.HTML(...)
// - django | iris.Django(...)
// - pug(jade) | iris.Pug(...)
// - handlebars | iris.Handlebars(...)
// - amber | iris.Amber(...)
tmpl := iris.HTML("./templates", ".html")
// 内置模板函数是:
//
// - {{ 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)
app.Get("/", hi)
// http://localhost:8080
app.Run(iris.Addr(":8080"))
}
func hi(ctx iris.Context) {
// 渲染模板文件 "./templates/hi.html"
ctx.View("hi.html")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<!-- 文件:./templates/hi.html -->
<b>{{greet "kataras"}}</b> <!-- 将会显示成这样:<b>Greetings kataras!</b> -->
1
2
3
2
3
# 嵌入
视图引擎也支持绑定(https://github.com/jteeuwen/go-bindata (opens new window)) 模板文件。 go-bindata
提供给你两个函数, Assset
和 AssetNames
,这些可以使用 .Binary
函数来对每一个模板引擎进行处理。
示例代码:
package main
import "github.com/kataras/iris"
func main() {
app := iris.New()
// $ go get -u github.com/jteeuwen/go-bindata/...
// $ go-bindata ./templates/...
// $ go build
// $ ./embedding-templates-into-app
// 不适用 Html 文件,你可以删除目录并运行这个例子
app.RegisterView(iris.HTML("./templates", ".html").Binary(Asset, AssetNames))
app.Get("/", hi)
// http://localhost:8080
app.Run(iris.Addr(":8080"))
}
type page struct {
Title, Name string
}
func hi(ctx iris.Context) {
// {{.Page.Title}} and {{Page.Name}}
ctx.ViewData("Page", page{Title: "Hi Page", Name: "iris"})
ctx.View("hi.html")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
这儿有一个真实的例子: https://github.com/kataras/iris/tree/master/_examples/view/embedding-templates-into-app (opens new window).
# 重载
允许在每个请求上自动重载模板。开发者开发模式时,不需要在每一次的模板编辑时都重启他们的应用,这个确实非常有用。
示例代码:
pugEngine := iris.Pug("./templates", ".jade")
pugEngine.Reload(true) // <--- 设置为 True,以便在每次请求时重新构建模板
app.RegisterView(pugEngine)
1
2
3
4
2
3
4
# 实例
- 概览 (opens new window)
- Hi (opens new window)
- 一个简单布局 (opens new window)
- 布局:
yield
和render
模板函数 (opens new window) urlpath
模板函数 (opens new window)url
模板函数 (opens new window)- 在处理器之间注入数据 (opens new window)
- 嵌入模板到应用程序的可执行文件中 (opens new window)
你也可以运行 quicktemplate (opens new window) 文件,简单的通过使用 context#ResponseWriter
, 可以看一下这个 iris/_examples/http_responsewriter/quicktemplate (opens new window) 例子。