【go語言學習】標準庫之template
一、模板和模板引擎
在基於MVC的web架構中,我們常常將不變的部分提出成為模板,可變部分通過後端程式提供資料,藉助模板引擎渲染來生成動態網頁。
模板可以理解為事先定義好的HTML文件檔案,模板渲染的作用機制可以簡單理解為文字替換操作—使用相應的資料去替換HTML文件中事先準備好的標記。
模板的誕生是為了將顯示與資料分離(即前後端分離),模板技術多種多樣,但其本質是將模板檔案和資料通過模板引擎生成最終的HTML程式碼。
模板引擎很多,Python的jinja,nodejs的jade等都很好。
二、go語言模板引擎
Go語言內建了文字模板引擎text/template和用於HTML文件的html/template。它們的作用機制可以簡單歸納如下:
- 模板檔案通常定義為
.tmpl
和.tpl
為字尾(也可以使用其他的字尾),必須使用UTF8編碼。 - 模板檔案中使用
{{
和}}
包裹和標識需要傳入的資料。 - 傳給模板的資料可以通過點號
.
來訪問,如果資料是複雜型別的資料,可以通過{{ .FieldName }}
來訪問它的欄位。 - 除
{{
和}}
包裹的內容外,其他內容均不做修改原樣輸出。
三、模板引擎的使用
Go語言模板引擎的使用可以分為三部分:定義模板檔案、解析模板檔案和模板渲染。
1、定義模板
按照相應的語法規則去編寫模板
2、解析模板
template包提供了以下方法解析模板,獲得模板物件
// 建立模板物件,併為其新增一個模板名稱 func New(name string) *Template {} // 解析字串 // 可以使用template.New("name").Parse(src string) // 來建立模板物件,並完成解析模板內容。 func (t *Template) Parse(src string) (*Template, error) {} // ParseFiles 方法可以解析模板檔案,並得到模板物件 func ParseFiles(filenames ...string) (*Template, error) {} // ParseGlob方法用於批量解析檔案 // 比如在當前目錄下有以h開頭的模板10個 // 使用template.ParseGlob("h*")即可頁將10個模板檔案一起解析出來 func ParseGlob(pattern string) (*Template, error) {}
3、模板渲染
template包提供了以下方法用於渲染模板。
func (t *Template) Execute(wr io.Writer, data interface{}) error {}
func (t *Template) ExecuteTemplate(wr io.Writer, name string, data interface{}) error {}
使用New在建立時就為其新增一個模板名稱,並且執行t.Execute()會預設去尋找該名稱進行資料融合。
使用ParseFiles一次指定多個檔案載入多個模板進來,就不可以使用t.Execute()來執行資料融合,可以通過t.ExecuteTemplate()方法指定模板名稱來執行資料融合。
4、基本示例
定義模板
// go_web/index.tmpl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p>hello {{ . }} </p>
</body>
</html>
解析和渲染模板
// go_web/main.go
package main
import (
"fmt"
"html/template"
"net/http"
)
func helloHandleFunc(w http.ResponseWriter, r *http.Request) {
// 2. 解析模板
t, err := template.ParseFiles("./index.tmpl")
if err != nil {
fmt.Println("template parsefile failed, err:", err)
return
}
// 3.渲染模板
name := "ruby"
t.Execute(w, name)
}
func main() {
http.HandleFunc("/", helloHandleFunc)
http.ListenAndServe(":8080", nil)
}
四、模板語法
{{
和}}
包裹的內容統稱為 action,分為兩種型別:
- 資料求值(data evaluations)
- 控制結構(control structures)
action 求值的結果會直接複製到模板中,控制結構和我們寫 Go 程式差不多,也是條件語句、迴圈語句、變數、函式呼叫等等…
1、註釋
{{/* a comment */}}
// 註釋,執行時會忽略。可以多行。註釋不能巢狀,並且必須緊貼分界符始止。
2、移除空格
在{{
符號的後面加上短橫線並保留一個或多個空格來去除它前面的空白(包括換行符、製表符、空格等),即{{- xxxx
。
在}}
的前面加上一個或多個空格以及一個短橫線-
來去除它後面的空白,即xxxx -}}
。
<p>{{ 20 }} < {{ 40 }}</p> // 20 < 40
<p>{{ 20 -}} < {{- 40 }}</p> // 20<40
3、管道pipeline
pipeline是指產生資料的操作。比如{{.}}、{{.Name}}、funcname args等。
可以使用管道符號|連結多個命令,用法和unix下的管道類似:|前面的命令將運算結果(或返回值)傳遞給後一個命令的最後一個位置。
{{"put" | printf "%s%s" "out" | printf "%q"}} // "output"
4、變數
在golang渲染template的時候,可以接受一個interface{}
型別的變數,我們在模板檔案中可以讀取變數內的值並渲染到模板裡。
{{
和}}
中間的 .
代表傳入的變數(資料),其代表當前作用域的當前物件,變數(資料)不同渲染不同。
有兩個常用的傳入變數的型別。一個是struct
,在模板內可以讀取該struct
的欄位(對外暴露的屬性)來進行渲染。還有一個是map[string]interface{}
,在模板內可以使用key
獲取對應的value
來進行渲染。
示例程式碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p>姓名:{{ .Name }}</p>
<p>年齡:{{ .Age }}</p>
<p>性別:{{ .Gender }}</p>
<p>語文成績:{{ .Score.yuwen}}</p>
<p>數學成績:{{ .Score.shuxue}}</p>
<p>英語成績:{{ .Score.yingyu}}</p>
</body>
</html>
package main
import (
"fmt"
"html/template"
"net/http"
)
// User 結構體
type User struct {
Name string
Age int
Gender string
Score map[string]float64
}
func indexHandleFunc(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("./index.tmpl")
if err != nil {
fmt.Println("template parsefiles failed, err:", err)
return
}
user := User{
Name: "ruby",
Age: 20,
Gender: "female",
Score: map[string]float64{
"yuwen": 98,
"shuxue": 100,
"yingyu": 94,
},
}
t.Execute(w, user)
}
func main() {
http.HandleFunc("/", indexHandleFunc)
http.ListenAndServe(":8080", nil)
}
自定義變數
{{ $obj := "jack" }}
{{ $obj }} // 輸出:jack
5、函式
golang的模板其實功能很有限,很多複雜的邏輯無法直接使用模板語法來表達,所以只能使用模板函式來實現。
首先,template包建立新的模板的時候,支援.Funcs方法來將自定義的函式集合匯入到該模板中,後續通過該模板渲染的檔案均支援直接呼叫這些函式。
該函式集合的定義為:
type FuncMap map[string]interface{}
key為方法的名字,value則為函式。這裡函式的引數個數沒有限制,但是對於返回值有所限制。有兩種選擇,一種是隻有一個返回值,還有一種是有兩個返回值,但是第二個返回值必須是error型別的。這兩種函式的區別是第二個函式在模板中被呼叫的時候,假設模板函式的第二個引數的返回不為空,則該渲染步驟將會被打斷並報錯。
- 內建模板函式:
var builtins = FuncMap{
// 返回第一個為空的引數或最後一個引數。可以有任意多個引數。
// "and x y"等價於"if x then y else x"
"and": and,
// 顯式呼叫函式。第一個引數必須是函式型別,且不是template中的函式,而是外部函式。
// 例如一個struct中的某個欄位是func型別的。
// "call .X.Y 1 2"表示呼叫dot.X.Y(1, 2),Y必須是func型別,函式引數是1和2。
// 函式必須只能有一個或2個返回值,如果有第二個返回值,則必須為error型別。
"call": call,
// 返回與其引數的文字表示形式等效的轉義HTML。
// 這個函式在html/template中不可用。
"html": HTMLEscaper,
// 對可索引物件進行索引取值。第一個引數是索引物件,後面的引數是索引位。
// "index x 1 2 3"代表的是x[1][2][3]。
// 可索引物件包括map、slice、array。
"index": index,
// 返回與其引數的文字表示形式等效的轉義JavaScript。
"js": JSEscaper,
// 返回引數的length。
"len": length,
// 布林取反。只能一個引數。
"not": not,
// 返回第一個不為空的引數或最後一個引數。可以有任意多個引數。
// "or x y"等價於"if x then x else y"。
"or": or,
"print": fmt.Sprint,
"printf": fmt.Sprintf,
"println": fmt.Sprintln,
// 以適合嵌入到網址查詢中的形式返回其引數的文字表示的轉義值。
// 這個函式在html/template中不可用。
"urlquery": URLQueryEscaper,
}
- 比較函式:
eq arg1 arg2:
arg1 == arg2時為true
ne arg1 arg2:
arg1 != arg2時為true
lt arg1 arg2:
arg1 < arg2時為true
le arg1 arg2:
arg1 <= arg2時為true
gt arg1 arg2:
arg1 > arg2時為true
ge arg1 arg2:
arg1 >= arg2時為true
- 自定義模板函式
t = t.Funcs(template.FuncMap{"handleFieldName": HandleFunc})
- 函式呼叫
{{funcname .arg1 .arg2}}
6、條件判斷
{{ if pipeline }} T1 {{ end }}
{{ if pipeline }} T1 {{ else }} T2 {{ end }}
{{ if pipeline }} T1 {{ else if pipeline }} T2 {{ end }}
7、迴圈遍歷
{{ range pipeline }} T1 {{ end }}
// 如果 pipeline 的長度為 0 則輸出 else 中的內容
{{ range pipeline }} T1 {{ else }} T2 {{ end }}
range可以遍歷slice、陣列、map或channel。遍歷的時候,會設定.
為當前正在遍歷的元素。
對於第一個表示式,當遍歷物件的值為0值時,則range直接跳過,就像if一樣。對於第二個表示式,則在遍歷到0值時執行else。
range的引數部分是pipeline,所以在迭代的過程中是可以進行賦值的。但有兩種賦值情況:
{{ range $value := pipeline }} T1 {{ end }}
{{ range $key, $value := pipeline }} T1 {{ end }}
如果range中只賦值給一個變數,則這個變數是當前正在遍歷元素的值。如果賦值給兩個變數,則第一個變數是索引值(array/slice是數值,map是key),第二個變數是當前正在遍歷元素的值。
8、with...end
{{ with pipeline }} T1 {{ end }}
{{ with pipeline }} T1 {{ else }} T0 {{ end }}
對於第一種格式,當pipeline不為0值的時候,將.
設定為pipeline運算的值,否則跳過。
對於第二種格式,當pipeline為0值時,執行else語句塊T0,否則.
設定為pipeline運算的值,並執行T1。
9、模板巢狀
- define
define可以直接在待解析內容中定義一個模板
// 定義名稱為name的template
{{ define "name" }} T {{ end }}
- template
使用template來執行模板
// 執行名為name的template
{{ template "name" }}
{{ template "name" pipeline }}
- block
{{ block "name" pipeline }} T {{ end }}
block等價於define定義一個名為name的模板,並在"有需要"的地方執行這個模板,執行時將.
設定為pipeline的值。
等價於:先 {{ define "name" }} T {{ end }}
再執行 {{ template "name" pipeline }}
。
程式碼示例:
<!-- index.tmpl -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
{{ template "content"}}
</body>
</html>
<!-- red.tmpl -->
{{ define "content" }}
<div style="color:red"><h3>hello world</h3></div>
{{ end }}
<!-- blue.tmpl -->
{{ define "content" }}
<div style="color:blue"><h3>hello world</h3></div>
{{ end }}
// main.go
package main
import (
"html/template"
"math/rand"
"net/http"
"time"
)
func indexHandleFunc(w http.ResponseWriter, r *http.Request) {
t := template.New("index.tmpl")
rand.Seed(time.Now().UnixNano())
if rand.Intn(100) > 50 {
t, _ = template.ParseFiles("./index.tmpl", "./red.tmpl")
} else {
t, _ = template.ParseFiles("./index.tmpl", "./blue.tmpl")
}
t.Execute(w, "")
}
func main() {
http.HandleFunc("/", indexHandleFunc)
http.ListenAndServe(":8080", nil)
}
如果使用block,那麼可以設定預設的content模板。
修改index.tmpl
<!-- index.tmpl -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
{{ block "content" . }}
<div style="color:yellow"><h3>hello world</h3></div>
{{ end }}
</body>
</html>
修改後端程式:
// main.go
package main
import (
"html/template"
"math/rand"
"net/http"
"time"
)
func indexHandleFunc(w http.ResponseWriter, r *http.Request) {
t := template.New("index.tmpl")
rand.Seed(time.Now().UnixNano())
if rand.Intn(100) > 75 {
t, _ = template.ParseFiles("./index.tmpl", "./red.tmpl")
} else if rand.Intn(100) > 25 {
t, _ = template.ParseFiles("./index.tmpl", "./blue.tmpl")
} else {
t, _ = template.ParseFiles("./index.tmpl")
}
t.Execute(w, "")
}
func main() {
http.HandleFunc("/", indexHandleFunc)
http.ListenAndServe(":8080", nil)
}
10、模板繼承
通過block、define、template實現模板繼承。
示例程式碼:
<!-- base.tmpl -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.head {
height: 50px;
background-color: red;
width: 100%;
text-align: center;
}
.main {
width: 100%;
}
.main .left {
width: 30%;
height: 1000px;
float: left;
background-color:violet;
text-align: center;
}
.main .right {
width: 70%;
float: left;
text-align: center;
height: 1000px;
background-color:yellowgreen;
}
</style>
</head>
<body>
<div class="head">
<h1>head</h1>
</div>
<div class="main">
<div class="left">
<h1>side</h1>
</div>
<div class="right">
{{ block "content" . }}
<h1>content</h1>
{{ end }}
</div>
</div>
</body>
</html>
<!-- index.tmpl -->
{{ template "base.tmpl" . }}
{{ define "content" }}
<h1>這是index頁面</h1>
{{ . }}
{{ end }}
<!-- home.tmpl -->
{{ template "base.tmpl" . }}
{{ define "content" }}
<h1>這是home頁面</h1>
{{ . }}
{{ end }}
// main.go
package main
import (
"html/template"
"net/http"
)
func indexHandleFunc(w http.ResponseWriter, r *http.Request) {
t := template.New("index.tmpl")
t, _ = t.ParseFiles("./base.tmpl", "./index.tmpl")
t.Execute(w, "index")
}
func homeHandleFunc(w http.ResponseWriter, r *http.Request) {
t := template.New("home.tmpl")
t, _ = t.ParseFiles("./base.tmpl", "./home.tmpl")
t.Execute(w, "home")
}
func main() {
server := http.Server{
Addr: "localhost:8080",
}
http.HandleFunc("/index", indexHandleFunc)
http.HandleFunc("/home", homeHandleFunc)
server.ListenAndServe()
}
11、修改預設的識別符號
Go標準庫的模板引擎使用的花括號{{
和}}
作為標識,而許多前端框架(如Vue和 AngularJS)也使用{{
和}}
作為識別符號,所以當我們同時使用Go語言模板引擎和以上前端框架時就會出現衝突,這個時候我們需要修改識別符號,修改前端的或者修改Go語言的。這裡演示如何修改Go語言模板引擎預設的識別符號:
template.New("test").Delims("{[", "]}").ParseFiles("./t.tmpl")
12、html/template的上下文感知
對於html/template包,有一個很好用的功能:上下文感知。text/template沒有該功能。
上下文感知具體指的是根據所處環境css、js、html、url的path、url的query,自動進行不同格式的轉義。
示例程式碼
<!-- index.tmpl -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>{{ . }}</div>
</body>
</html>
// main.go
package main
import (
// "text/template"
"html/template"
"net/http"
)
func indexHandleFunc(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("./index.tmpl")
data := `<script>alert("helloworld")</script>`
t.Execute(w, data)
}
func main() {
http.HandleFunc("/", indexHandleFunc)
http.ListenAndServe(":8080", nil)
}
執行程式,頁面顯示:
不轉義
上下文感知的自動轉義能讓程式更加安全,比如防止XSS攻擊(例如在表單中輸入帶有的內容並提交,會使得使用者提交的這部分script被執行)。
如果確實不想轉義,可以進行型別轉換。
type CSS
type HTML
type JS
type URL
編寫一個自定義的模板函式,實現對內容的型別轉換。
示例程式碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>{{ .str1 }}</div>
<div>{{ .str2 | safe }}</div>
</body>
</html>
package main
import (
// "text/template"
"html/template"
"net/http"
)
func indexHandleFunc(w http.ResponseWriter, r *http.Request) {
t := template.New("index.tmpl")
t.Funcs(template.FuncMap{
"safe": func(str string) template.HTML {
return template.HTML(str)
},
}).ParseFiles("./index.tmpl")
m := map[string]interface{}{
"str1": `<script>alert("helloworld")</script>`,
"str2": `<a href = "http://baidu.com">baidu</a>`,
}
t.Execute(w, m)
}
執行程式,頁面顯示: