Go模板渲染

Template渲染

Golang原生支持对Template的数据渲染(text/template)

Go:

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
package main

import (
"fmt"
"net/http"
"text/template"
)

// 模板

// Person 结构体
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}

func templateHandle(respWrite http.ResponseWriter, req *http.Request) {
// 准备渲染数据
p := Person{
Name: "张三",
Age: 20,
}
// 加载html文件
temp, err := template.ParseFiles("./go_dev/day10/example7/main/index.html")
if err != nil {
fmt.Println("parse file err:", err)
return
}
// 把数据渲染到模板
err = temp.Execute(respWrite, p)
if err != nil {
fmt.Println("template Execute err:", err)
return
}
}

func main() {
http.HandleFunc("/", templateHandle)
err := http.ListenAndServe(":8888", nil)
if err != nil {
fmt.Println("listen err:", err)
return
}
}

index.html:

1
2
3
4
5
6
7
8
9
10
11
12
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>hi {{.Name}}</h1>
</body>
</html>

Template 条件语句

  • if判断:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <html>
    <head>
    </head>
    <body>
    {{if gt .Age 18}}
    <p>hello, old man, {{.Name}}</p>
    {{else}}
    <p>hello,young man, {{.Name}}</p>
    {{end}}
    </body>
    </html>
    常见操作符:
    • not 非
      1
      2
      {{if not .condition}} 
      {{end}}
    • and 与
      1
      2
      {{if and .condition1 .condition2}} 
      {{end}}
    • or 或
      1
      2
      {{if or .condition1 .condition2}} 
      {{end}}
    • eq 等于
      1
      2
      {{if eq .var1 .var2}} 
      {{end}}
    • ne 不等于
      1
      2
      {{if ne .var1 .var2}} 
      {{end}}
    • lt 小于 (less than)
      1
      2
      {{if lt .var1 .var2}} 
      {{end}}
    • le 小于等于
      1
      2
      {{if le .var1 .var2}} 
      {{end}}
    • gt 大于
      1
      2
      {{if gt .var1 .var2}} 
      {{end}}
    • ge 大于等于
      1
      2
      {{if ge .var1 .var2}} 
      {{end}
  • range循环
    1
    2
    3
    4
    5
    6
    7
     {{range .}}
    {{if gt .Age 18}}
    <p>hello, old man, {{.Name}}</p>
    {{else}}
    <p>hello,young man, {{.Name}}</p>
    {{end}}
    {{end}}

    例子:

  • main.go:
    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
    47
    48
    49
    50
    51
    52
    53
    54
    55
    package main

    import (
    "fmt"
    "net/http"
    "text/template"
    )

    type Person struct {
    Name string
    Age int
    }

    var temp *template.Template

    // 模板-条件渲染

    func userInfo(respWrite http.ResponseWriter, req *http.Request) {
    // 加载模板
    temp, err := template.ParseFiles("./go_dev/day10/example8/main/index.html")
    if err != nil {
    fmt.Println("load template file err:", err)
    return
    }

    // 准备渲染数据
    var pArr []Person

    p1 := Person{
    Name: "张三",
    Age: 18,
    }

    p2 := Person{
    Name: "张四",
    Age: 21,
    }
    pArr = append(pArr, p1)
    pArr = append(pArr, p2)
    // 把数据渲染到模板
    err = temp.Execute(respWrite, pArr)
    if err != nil {
    fmt.Println("template execute err:", err)
    }
    }

    func main() {
    http.HandleFunc("/user", userInfo)
    err := http.ListenAndServe(":8888", nil)
    if err != nil {
    fmt.Println("http listen err:", err)
    return
    }
    }

  • index.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
    <!DOCTYPE html>
    <html lang="zh-cn">

    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    </head>

    <body>
    <!-- 循环 range-->
    {{range .}}
    <h1>hi {{.Name}}</h1>
    <!-- 条件判断 -->
    {{if gt .Age 18}}
    <p>Your age is {{.Age}} and you are an adult.</p>
    {{else}}
    <p>Your age is {{.Age}} and you are a minor.</p>
    {{end}}
    {{end}}
    </body>

    </html>