Go使用Text和HTML模板

简介

Go的两个包text/templatehtml/template提供了将变量的值替换到文本的机制。通常用在一些printf应付不了的、需要复杂的格式化的场合。

模板的使用

模板规则

Action

模板是一个包含了一个或多个双花括号标记{{...}})的字符串,这种双花括标记号叫做Action。模板中所有的除Action的字符串都按照原样打印,Action则表现了特殊的行为。通过Action可以打印变量的值、字段的值、调用函数或方法、表达控制流程等。

Dot

Action中的点(.)表示当前值。初始值为传递给模板的参数。

API

  • text/template与html/template提供了相同的API来创建、解析和执行模板,区别是html/template提供了HTML转义。

    // 创建一个名字为name的模板
    func New(name string) *Template
    
    // 解析模板字符串
    func (t *Template) Parse(text string) (*Template, error)
    // 解析文件
    func ParseFiles(filenames ...string) (*Template, error)
    
    // 执行模板,将结果写入wr
    func (t *Template) Execute(wr io.Writer, data interface{}) error
  • 在确定模板可以正确解析的场合,可以使用Must()来处理Parse()的结果。如果Parse()失败,则调用panic(),如果成功,返回Template对象。

    func Must(t *Template, err error) *Template
  • Funcs()用来注册函数给模板,注册之后模板就可以通过名字调用外部函数了(见下面例子)。

    func (t *Template) Funcs(funcMap FuncMap) *Template
    
    type FuncMap map[string]interface{}
  • 输入

例子

最基本的使用

package main

import (
    "log"
    "os"
    "text/template" // 引入包,如果处理HTML,则为 "html/template"
)

func main() {
    // 创建一个名字为report的模板,并解析一个字符串
    t, err := template.New("report").Parse("I am {{.}} years old.")
    if err != nil {
        log.Fatal(err)
    }
    // 执行模板,5作为参数传递,将结果写到标准输出
    t.Execute(os.Stdout, 5)

    // 打印结果:I am 5 years old.
}

访问复合数据结构的值

访问结构体字段需要在点(.)后面加字段名。
访问Map值需要在点(.)后面加Key的值。

package main

import (
    "os"
    "text/template"
)

func main() {
    t := template.Must(
        template.New("report").Parse("I am {{.Age}} years old.\n"))
    t.Execute(os.Stdout, struct{ Age int }{5})
    t.Execute(os.Stdout, map[string]int{"Age": 5})
}

需要注意的是template访问结构体字段时利用了反射机制,因此必须导出字段才能在模板里访问。

循环

使用{{range}}{{end}}来表示循环。每次迭代中点(.)代表当前迭代的值。

package main

import (
    "os"
    "text/template"
)

func main() {
    const temStr = "{{range .}}{{.}}\n{{end}}"
    t := template.Must(template.New("report").Parse(temStr))
    t.Execute(os.Stdout, []string{"Hello", "Gopher"})
}

管道

|将其左边的输出作为其右边的输入。

package main

import (
    "os"
    "text/template"
)

func main() {
    const temStr = `{{. | printf "% 10s"}}`
    t := template.Must(template.New("report").Parse(temStr))
    t.Execute(os.Stdout, "Gopher")
}

调用函数

如果在模板中需要调用函数,则在Parse()之前需要通过Funcs()来注册。

package main

import (
    "os"
    "text/template"
)

func greet(name string) string {
    return "Hello, " + name + "!"
}

func main() {
    const temStr = `{{. | greet}}`
    t := template.Must(template.New("report").
        Funcs(template.FuncMap{"greet": greet}).
        Parse(temStr))
    t.Execute(os.Stdout, "Gopher")
}

生成动态HTML

  • HTML文件:time.html

    <html>
        <body>
            <p>Current time is: {{.}}</p>
        </body>
    </html>
  • go文件:

    package main
    
    import (
        "html/template"
        "log"
        "net/http"
        "time"
    )
    
    func main() {
        http.HandleFunc("/time", timeHandler)
        log.Fatal(http.ListenAndServe(":9090", nil))
    }
    
    func timeHandler(w http.ResponseWriter, r *http.Request) {
        t := template.Must(template.ParseFiles("time.html"))
        t.Execute(w, time.Now().Format("2006-01-02 15:04:05"))
    }
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值