扫码订阅《 》或入驻星球,即可阅读文章!

GOLANG ROADMAP

阅读模式

  • 沉浸
  • 自动
  • 日常
首页
Go友会
  • 城市
  • 校园
Go学院
  • Go小课
  • Go小考
  • Go实战
  • 精品课
Go求职
  • 求职辅导🔥
  • Offer收割社群
  • 企业题库
  • 面试宝典
Go宝典
  • 在线宝典
  • B站精选
  • 推荐图书
  • 每日博文
Go仓库
实验区
  • Go周边
  • Go下载
  • Go月刊
消息
更多
  • 用户中心

    • 我的信息
    • 推广返利
  • 玩转星球

    • 星球介绍
    • 角色体系
    • 星主权益
  • 支持与服务

    • 联系星主
    • 成长记录
    • 常见问题
    • 吐槽专区
  • 合作交流

    • 渠道合作
    • 课程入驻
    • 友情链接
author-avatar

GOLANG ROADMAP


首页
Go友会
  • 城市
  • 校园
Go学院
  • Go小课
  • Go小考
  • Go实战
  • 精品课
Go求职
  • 求职辅导🔥
  • Offer收割社群
  • 企业题库
  • 面试宝典
Go宝典
  • 在线宝典
  • B站精选
  • 推荐图书
  • 每日博文
Go仓库
实验区
  • Go周边
  • Go下载
  • Go月刊
消息
更多
  • 用户中心

    • 我的信息
    • 推广返利
  • 玩转星球

    • 星球介绍
    • 角色体系
    • 星主权益
  • 支持与服务

    • 联系星主
    • 成长记录
    • 常见问题
    • 吐槽专区
  • 合作交流

    • 渠道合作
    • 课程入驻
    • 友情链接
  • Iris框架中文文档

    • 概要
    • 功能列表
    • 安装
    • HTTP 主机配置
    • 配置信息
    • HTTP 路由
    • Context机制
    • 动态路由参数
    • 路由命名
    • 路由中间件
    • 打包Router
    • 错误处理
    • MVC 架构
    • MVC电影项目示例
    • MVC之Websockets
    • MVC中使用会话
    • 单一控制器
    • 视图(模板引擎)
    • 会话Sessions
    • Websocket

扫码订阅《 》或入驻星球,即可阅读文章!

视图(模板引擎)


GOLANG ROADMAP

# 视图

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
<!-- 文件: ./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

# 模板函数

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

1
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

这儿有一个真实的例子: 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

# 实例

  • 概览 (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) 例子。

  • 视图
  • 概览
  • 模板函数
  • 嵌入
  • 重载
  • 实例