视图(模板引擎)

未匹配的标注

视图

Iris支持 5 种开箱即用的模板引擎,开发者依然可以选择使用任何 Golang 之外的模板引擎, 像 context/context#ResponseWriter() 是一个 io.Writer

所有这五个模板引擎的公共 API 都具有相同的特性,像布局,模板函数,局部定制布局,部分展示等。

概览

// 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"))
}
<!-- 文件: ./views/hello.html -->
<html>
<head>
    <title>Hello Page</title>
</head>
<body>
    <h1>{{.message}}</h1>
</body>
</html>

模板函数

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")
}
<!-- 文件:./templates/hi.html -->
<b>{{greet "kataras"}}</b> <!-- 将会显示成这样:<b>Greetings kataras!</b> -->

嵌入

视图引擎也支持绑定(github.com/jteeuwen/go-bindata) 模板文件。 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")
}

这儿有一个真实的例子: github.com/kataras/iris/tree/maste....

重载

允许在每个请求上自动重载模板。开发者开发模式时,不需要在每一次的模板编辑时都重启他们的应用,这个确实非常有用。

示例代码:

pugEngine := iris.Pug("./templates", ".jade")
pugEngine.Reload(true) // <--- 设置为 True,以便在每次请求时重新构建模板
app.RegisterView(pugEngine)

实例

你也可以运行 quicktemplate 文件,简单的通过使用 context#ResponseWriter, 可以看一下这个 iris/_examples/http_responsewriter/quickte... 例子。

本文章首发在 LearnKu.com 网站上。

本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

原文地址:https://learnku.com/docs/iris-go/10/view...

译文地址:https://learnku.com/docs/iris-go/10/view...

上一篇 下一篇