Iris 通过它的视图引擎提供了 6 种开箱即用的模板解析器。当然开发者仍然可以使用各种go模板解析器作为 Context.ResponseWriter(), 只要实现了 http.ResponseWriterio.Writer

    Iris提出了一些默认的解析器不支持的通用规则和功能。例如,我们已经支持 yieldrenderrender_rcurrenturlpath 模板函数, 并通过中间件和嵌入式模板文件为所有的引擎布局和绑定。

    为了使用一个模板引擎独特的功能,你需要通过阅读文档来学习模板引擎的特征和语法(点击下面)。选择最适合你app需要的。

    内置的视图引擎:

    • 标准 template/html:iris.HTML(...)
    • django:iris.Django(...)
    • handlebars:iris.Handlebars(...)
    • amber:iris.Amber(...)
    • pug(jade):iris.Pug(...)
    • jet:iris.Jet(...)

    一个或者多个视图引擎可以注册到相同的应用中。使用 RegisterView(ViewEngine) 方法注册。

    ./views 目录中加载所有后缀为 .html 的模板,然后使用标准库 html/template 包来解析它们。

    1. // [app := iris.New...]
    2. tmpl := iris.HTML("./views", ".html")
    3. app.RegisterView(tmpl)

    在路由的处理器中用 Context.View 方法渲染或者执行一个视图。

    1. ctx.View("hi.html")

    在使用 Context.View 之前使用 Context.ViewData 方法绑定一个Go的键值对。

    绑定 {{.message}}hello world

    1. ctx.ViewData("message", "Hello world!")

    你有两种方法绑定一个go模型:

    • 第一种
    1. ctx.ViewData("user", User{})
    2. // variable binding as {{.user.Name}}
    • 第二种
    1. ctx.View("user-page.html", User{})
    2. // root binding as {{.Name}}

    要添加一个模板函数, 请使用首选视图引擎的 AddFunc 方法。

    1. // func name, input arguments, render value
    2. tmpl.AddFunc("greet", func(s string) string {
    3. return "Greetings " + s + "!"
    4. })

    要重新加载本地文件更改,请调用视图引擎的 Reload 方法。

    1. tmpl.Reload(true)

    使用嵌入式的模板并且不依赖本地文件系统,使用 go-bindata 外部工具,然后把AssetAssetName 函数传递到首选视图引擎的 Binary 方法。

    1. tmpl.Binary(Asset, AssetNames)

    示例代码,请阅读注释:

    1. // file: main.go
    2. package main
    3. import "github.com/kataras/iris/v12"
    4. func main() {
    5. app := iris.New()
    6. // Parse all templates from the "./views" folder
    7. // where extension is ".html" and parse them
    8. // using the standard `html/template` package.
    9. tmpl := iris.HTML("./views", ".html")
    10. // Enable re-build on local template files changes.
    11. tmpl.Reload(true)
    12. // Default template funcs are:
    13. //
    14. // - {{ urlpath "myNamedRoute" "pathParameter_ifNeeded" }}
    15. // - {{ render "header.html" }}
    16. // and partial relative path to current page:
    17. // - {{ render_r "header.html" }}
    18. // - {{ yield }}
    19. // - {{ current }}
    20. // Register a custom template func:
    21. tmpl.AddFunc("greet", func(s string) string {
    22. return "Greetings " + s + "!"
    23. })
    24. // Register the view engine to the views,
    25. // this will load the templates.
    26. app.RegisterView(tmpl)
    27. // Method: GET
    28. // Resource: http://localhost:8080
    29. app.Get("/", func(ctx iris.Context) {
    30. // Bind: {{.message}} with "Hello world!"
    31. ctx.ViewData("message", "Hello world!")
    32. // Render template file: ./views/hi.html
    33. ctx.View("hi.html")
    34. })
    35. app.Run(iris.Addr(":8080"))
    36. }

    hi.html:

    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>

    浏览器源码:

    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>