go模板

 

 

 

https://mp.weixin.qq.com/s?__biz=Mzg5MzYwODEyMQ==&mid=2247487091&idx=1&sn=e88a2c07fab96f1e1a689c5afea25202&chksm=c02d07a5f75a8eb3612be516d447e4a3493558393c6df6ea2ca923c53148d8ef1c11ced580f9&mpshare=1&srcid=0211v6deOghJlYeEkFW7Dhvl&sharer_sharetime=1644539178357&sharer_shareid=4a5c1c53c0e1f5acdc29a0de07832a03&from=singlemessage&scene=1&subscene=10000&clicktime=1644632035&enterid=1644632035&ascene=1&devicetype=android-29&version=28001339&nettype=cmnet&abtest_cookie=AAACAA%3D%3D&lang=zh_CN&exportkey=Ax42HZDpsbVSFhOetFga930%3D&pass_ticket=ZLaJb%2FKBFL6l6DJIqtPoJzBEMq0Xiw1IwvmG1Ijke2QxzAoTeE0Q9NhRc0Eia6MA&wx_header=3

Action(动作) 就是一些嵌入在 Go 模板中的命令,它位于两组花括号之间 {{ }} 。我们之前提到的 . 就是一个 Action ,而且是最重要的一个,它代表了传入模板的数据。Action 主要可以分为下面几种类型:条件类 、 迭代/遍历类 、 设置类 、 包含类 。下面就来分别介绍这几类 Action 。

 

图片

条件类 Action

图片

 

条件类 Action 形式如下:

{{ if arg }}
  some content
{{ end }}

其中, arg 我们可以理解为一个值或者一个变量,如果 arg 返回的是 true ,那么中间的内容就会显示出来。还有另一个形式如下:

{{ if arg }}
  some content
{{ else }}
  other content
{{ end }}

如果 arg 返回不为空,这里的空指的是 false 、 0 、空指针或接口、长度为 0 的数组、切片、map 或 字符串,那么 if 中间的内容就会显示出来,否则 else 中间的内容就会显示出来。下面是一个例子,模板 template.html 如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Action</title>
</head>
<body>
    {{ if . }}
    True!
    {{ else }}
    False!
    {{ end }}
</body>
</html>

当传入参数为 true 的情况下,网页会显示 True! ,而 false 的情况下,网页会显示 False! 。main.go 代码如下:

package main

import (
 "html/template"
 "net/http"
)

func main() {
 server := http.Server{
  Addr: "localhost:8080",
 }
 http.HandleFunc("/action", func (w http.ResponseWriter, r *http.Request) {
  t, _ := template.ParseFiles("template.html")
  t.Execute(w, true)
 })
 server.ListenAndServe()
}

运行上面程序,访问 http://localhost:8080/action ,会显示 True! ,因为我们传入的数据为 true 。

 

图片

迭代/遍历类 Action

图片

 

迭代/遍历类 Action 形式如下:

{{ range array }}
  Dot is set to the element {{ . }}
{{ end }}

迭代/遍历类 Action 用来遍历数组、slice、map 或 channel 等数据结构, range 为关键字, . 表示每次迭代/遍历中的元素。下面的例子 template.html 文件中 {{ range . }} 的 . 表示的是传入的数据,而 {{ . }} 中的 . 表示的是每次迭代/遍历中的元素。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Action</title>
</head>
<body>
    <ul>
        {{ range . }}
        <li>{{ . }}</li>
        {{ end }}
    </ul>
</body>
</html>

main.go 修改如下,这里添加了一个 fruits 字符串切片,并将其作为数据传入:

package main

import (
 "html/template"
 "net/http"
)

func main() {
 server := http.Server{
  Addr: "localhost:8080",
 }
 http.HandleFunc("/action", func (w http.ResponseWriter, r *http.Request) {
  t, _ := template.ParseFiles("template.html")
  fruits := []string{"orange", "apple", "banana", "pineapple", "grapes", "pears", "cherry"}
  t.Execute(w, fruits)
 })
 server.ListenAndServe()
}

运行上面程序,访问 http://localhost:8080/action ,会得到下面的结果:

orange
apple
banana
pineapple
grapes
pears
cherry

迭代/遍历类 Action 还有一种回落机制,也就是当要遍历的集合为空时,能使用 {{ else }} 做适当的 Action 。例如我们将 template.html 修改如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Action</title>
</head>
<body>
    <ul>
        {{ range . }}
        <li>{{ . }}</li>
        {{ else }}
        <li> Nothing </li>
        {{ end }}
    </ul>
</body>
</html>

将 main.go 传入的数据改为空的字符串切片:

package main

import (
 "html/template"
 "net/http"
)

func main() {
 server := http.Server{
  Addr: "localhost:8080",
 }
 http.HandleFunc("/action", func (w http.ResponseWriter, r *http.Request) {
  t, _ := template.ParseFiles("template.html")
  fruits := []string{}
  t.Execute(w, fruits)
 })
 server.ListenAndServe()
}

运行上面程序,访问 http://localhost:8080/action ,会显示 Nothing 。

 

图片

设置类 Action

图片

 

设置类 Action 形式如下:

{{ with arg }}
  Dot is set to arg
{{ end }}

设置类 Action 允许在指定范围内,让 . 来表示其它指定的值(arg),也就是说这个范围内的 arg 不表示上下文传入模板的数据,而是表示 arg 这个参数。例如:

package main

import (
 "html/template"
 "net/http"
)

func main() {
 server := http.Server{
  Addr: "localhost:8080",
 }
 http.HandleFunc("/action", func (w http.ResponseWriter, r *http.Request) {
  t, _ := template.ParseFiles("template.html")
  t.Execute(w, "Go")
 })
 server.ListenAndServe()
}

在 main.go 中,我们只传入了数据 Go 这个字符串,在 template.html 中使用 {{ with "Web" }} 临时将参数改为 Web ,这只在该范围内有效。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Action</title>
</head>
<body>
    <div>
        The dot is {{ . }}
    </div>
    <div>
        {{ with "Web" }}
        The dot is set to {{ . ]}
        {{ end }}
    </div>
    <div>
        The dot is {{ . }} again
    </div>
</body>
</html>

运行上面程序,访问 http://localhost:8080/action ,显示结果如下:

The dot is Go
The dot is set to Web
The dot is Go again

设置类 Action 也有回落机制,把 template.html 修改如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Action</title>
</head>
<body>
    <div>The dot is {{ . }}</div>
    <div>
        {{ with "" }}
        The dot is set to {{ . ]}
        {{ else }}
        The dot is still {{ . }}
        {{ end }}
    </div>
    <div>The dot is {{ . }} again</div>
</body>
</html>

最终结果显示为:

The dot is Go
The dot is still Go
The dot is Go again

 

包含类 Action

 

包含类 Action 形式如下:

{{ template "name" }}

这里的 name 为模板名,它允许你在模板中包含其它的模板,例如 template1.html 如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Action</title>
</head>
<body>
    <div>This is template1.html</div>
    <div>This is the value of the dot in template1.html : [{{ . }}]</div>
    <hr />
    {{ template "template2.html" }}
    <hr />
    <div>This is template1.html after</div>
</body>
</html>

template2.html 如下:

<div>
  This is template2.html
  <br>
  This is the value of the dot in template2.html : [{{ . }}]
</div>

main.go 如下:

package main

import (
 "html/template"
 "net/http"
)

func main() {
 server := http.Server{
  Addr: "localhost:8080",
 }
 http.HandleFunc("/action", func (w http.ResponseWriter, r *http.Request) {
  t, _ := template.ParseFiles("template1.html", "template2.html")
  t.Execute(w, "Go")
 })
 server.ListenAndServe()
}

运行上面程序,访问 http://localhost:8080/action ,显示结果如下:

This is template1.html
This is the value of the dot in template1.html : [Go]
—————————————————————————————————————————————————————
This is template2.html
This is the value of the dot in template2.html : []
—————————————————————————————————————————————————————
This is template1.html after

包含类 Action 还有另一种形式:

{{ template "name" arg }}

这里的 arg 指的是给被包含模板传递的参数。修改上面的例子,添加参数 {{ template "template2.html" . }} :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Action</title>
</head>
<body>
    <div>This is template1.html</div>
    <div>This is the value of the dot in template1.html : [{{ . }}]</div>
    <hr />
    {{ template "template2.html" . }}
    <hr />
    <div>This is template1.html after</div>
</body>
</html>

运行上面程序,访问 http://localhost:8080/action ,显示结果如下:

This is template1.html
This is the value of the dot in template1.html : [Go]
—————————————————————————————————————————————————————
This is template2.html
This is the value of the dot in template2.html : [Go]
—————————————————————————————————————————————————————
This is template1.html after
posted @ 2022-02-12 10:05  技术颜良  阅读(181)  评论(0编辑  收藏  举报