进阶 www.iris-go.com 2026-10-20 02:39:14 · 19 阅读
第71章 Iris 框架第71章:View 与 Templates 模板系统详解
View/Templates
Iris 提供了一套功能强大的模板系统,支持多种模板引擎。
基本用法
```go
package main
import "github.com/kataras/iris/v12"
func main() {
app := iris.New()
// 注册视图引擎
app.RegisterView(iris.HTML("./views", ".html"))
app.Get("/", func(ctx iris.Context) {
// 渲染模板并传入数据
ctx.View("index.html", iris.Map{
"title": "Hello World",
"message": "Welcome to Iris!",
})
})
app.Listen(":8080")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
模板引擎 HTML 模板 ```go // 注册 HTML 模板 tmpl := iris.HTML("./views", ".html") tmpl.Layout("layouts/main.html") tmpl.Reload(true) // 启用重载以便开发 app.RegisterView(tmpl) // 在处理程序中使用 app.Get("/", func(ctx iris.Context) { ctx.ViewData("title", "Home Page") ctx.View("home.html") }) 1
2
3
4
5
6
7
8
9
10
11
Django 模板 ```go import "github.com/kataras/iris/v12/view/django" // 注册 Django 模板 tmpl := django.New("./views", ".html") tmpl.Reload(true) app.RegisterView(tmpl) 1
2
3
4
5
6
Pug 模板 ```go import "github.com/kataras/iris/v12/view/pug" // 注册 Pug 模板 tmpl := pug.New("./views", ".pug") tmpl.Reload(true) app.RegisterView(tmpl) 1
2
3
4
5
6
布局(Layouts) ```go // 设置默认布局 tmpl := iris.HTML("./views", ".html") tmpl.Layout("layouts/main.html") app.RegisterView(tmpl) // 为特定路由覆盖布局 app.Get("/custom", func(ctx iris.Context) { ctx.ViewLayout("layouts/custom.html") ctx.View("page.html") }) // 禁用特定路由的布局 app.Get("/no-layout", func(ctx iris.Context) { ctx.ViewLayout(nil) ctx.View("page.html") }) 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
模板函数 ```go tmpl := iris.HTML("./views", ".html") // 添加自定义模板函数 tmpl.AddFunc("greet", func(name string) string { return "Hello " + name + "!" }) app.RegisterView(tmpl) // 在模板中使用 // {{ greet "John" }} 1
2
3
4
5
6
7
8
9
10
11
局部模板(Partial Templates) ```go // 注册局部模板 tmpl := iris.HTML("./views", ".html") tmpl.AddFunc("partial", tmpl.Partial) app.RegisterView(tmpl) // 在模板中使用 // {{ partial "header.html" . }} 1
2
3
4
5
6
7
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
模板引擎 HTML 模板 ```go // 注册 HTML 模板 tmpl := iris.HTML("./views", ".html") tmpl.Layout("layouts/main.html") tmpl.Reload(true) // 启用重载以便开发 app.RegisterView(tmpl) // 在处理程序中使用 app.Get("/", func(ctx iris.Context) { ctx.ViewData("title", "Home Page") ctx.View("home.html") }) 1
2
3
4
5
6
7
8
9
10
11
Django 模板 ```go import "github.com/kataras/iris/v12/view/django" // 注册 Django 模板 tmpl := django.New("./views", ".html") tmpl.Reload(true) app.RegisterView(tmpl) 1
2
3
4
5
6
Pug 模板 ```go import "github.com/kataras/iris/v12/view/pug" // 注册 Pug 模板 tmpl := pug.New("./views", ".pug") tmpl.Reload(true) app.RegisterView(tmpl) 1
2
3
4
5
6
布局(Layouts) ```go // 设置默认布局 tmpl := iris.HTML("./views", ".html") tmpl.Layout("layouts/main.html") app.RegisterView(tmpl) // 为特定路由覆盖布局 app.Get("/custom", func(ctx iris.Context) { ctx.ViewLayout("layouts/custom.html") ctx.View("page.html") }) // 禁用特定路由的布局 app.Get("/no-layout", func(ctx iris.Context) { ctx.ViewLayout(nil) ctx.View("page.html") }) 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
模板函数 ```go tmpl := iris.HTML("./views", ".html") // 添加自定义模板函数 tmpl.AddFunc("greet", func(name string) string { return "Hello " + name + "!" }) app.RegisterView(tmpl) // 在模板中使用 // {{ greet "John" }} 1
2
3
4
5
6
7
8
9
10
11
局部模板(Partial Templates) ```go // 注册局部模板 tmpl := iris.HTML("./views", ".html") tmpl.AddFunc("partial", tmpl.Partial) app.RegisterView(tmpl) // 在模板中使用 // {{ partial "header.html" . }} 1
2
3
4
5
6
7
错误处理 goapp.Get("/", func(ctx iris.Context) {
err := ctx.View("page.html", data)
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.WriteString("Template Error")
return
}
})1
2
3
4
5
6
7
8
缓存 gotmpl := iris.HTML("./views", ".html")
// 启用模板缓存
tmpl.Reload(false) // 关闭热加载
app.RegisterView(tmpl)1
2
3
4
5