基本使用方法
- 新建或者加载一个模板对象
1
2
3
4
|
type Template
func New(name string) *Template
func ParseFiles(filenames ...string) (*Template, error)
func Must(t *Template, err error) *Template
|
即:比如: t := template.New(“test template”)
- 加载模板, 定义动态数据占位符
1
2
|
func (t *Template) Parse(text string) (*Template, error)
func (t *Template) ParseFiles(filenames ...string) (*Template, error)
|
即:比如
1
2
3
4
5
6
7
8
|
var templateText = `This is a test for template:
Name: {{.Name}}
Age: {{.Age}}
School: {{.School}}
Married: {{.MarriedOK}}
`
t, err = t.Parse(templateText)
|
- 定义动态数据,执行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
var example = struct {
Name string
Age int
School string
MarriedOK bool
}{
Name: "xiewei",
Age: 18,
School: "shanghaiUniversity",
MarriedOK: true,
}
t.Execute(os.Stdout, example)
|
结果:
1
2
3
4
|
Name: xiewei
Age: 18
School: shanghaiUniversity
Married: true
|
传统struct传递参数的方法
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
42
43
44
45
46
|
package main
import (
"log"
"os"
"text/template"
)
func main() {
// Define a template.
const letter = `
Dear {{.Name}},
{{if .Attended}}
It was a pleasure to see you at the wedding.
{{- else}}
It is a shame you couldn't make it to the wedding.
{{- end}}
{{with .Gift -}}
Thank you for the lovely {{.}}.
{{end}}
Best wishes,
Josie
`
// Prepare some data to insert into the template.
type Recipient struct {
Name, Gift string
Attended bool
}
var recipients = []Recipient{
{"Aunt Mildred", "bone china tea set", true},
{"Uncle John", "moleskin pants", false},
{"Cousin Rodney", "", false},
}
// Create a new template and parse the letter into it.
t := template.Must(template.New("letter").Parse(letter))
// Execute the template for each recipient.
for _, r := range recipients {
err := t.Execute(os.Stdout, r)
if err != nil {
log.Println("executing template:", err)
}
}
}
|
结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
Dear Aunt Mildred,
It was a pleasure to see you at the wedding.
Thank you for the lovely bone china tea set.
Best wishes,
Josie
Dear Uncle John,
It is a shame you couldn't make it to the wedding.
Thank you for the lovely moleskin pants.
Best wishes,
Josie
Dear Cousin Rodney,
It is a shame you couldn't make it to the wedding.
Best wishes,
Josie
Program exited.
|
动态传递参数map[string]interface
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
|
package main
import (
"bytes"
"fmt"
"log"
"text/template"
)
func main() {
// Define a template.
const letter = `
Dear {{.Name}},
{{if .Attended}}
It was a pleasure to see you at the wedding.
{{- else}}
It is a shame you couldn't make it to the wedding.
{{- end}}
{{with .Gift -}}
Thank you for the lovely {{.}}.
{{end}}
Best wishes,
Josie
`
// Create a new template and parse the letter into it.
t := template.Must(template.New("letter").Parse(letter))
var data map[string]interface{} = map[string]interface{}{
"Name": "Li xp",
"Gift": "i Macbook pro",
"Attended": true,
}
var out bytes.Buffer
err := t.Execute(&out, data)
if err != nil {
log.Println("executing template:", err)
}
fmt.Printf("success %v\n", out.String())
}
|
如何获取template渲染的结果
你看 template 渲染的函数签名:Execute(wr io.Writer, data interface{}) error
显然渲染内容可输出到实现 io.Writer 接口的任何地方,比如 os.Stdout,文件,buffer 等地方
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
type User struct {
Name string
}
func main() {
tpl := template.New("example")
tpl, _ = tpl.Parse("<p> hello {{.Name}} </p>")
data := User{Name: "Tom"}
var buf bytes.Buffer
if err := tpl.Execute(&buf, data); err != nil {
log.Fatal(err)
}
fmt.Println(buf.String()) // 渲染后的字符串 // <p> hello Tom </p>
}
|
参考
详解golang 模板(template)的常用基本语法
Golang利用自定义模板发送邮件的方法详解