一、模板和模板引擎
在基于MVC的web架構中,我們常常將不變的部分提出成為模板,可變部分通過后端程序提供數據,借助模板引擎渲染來生成動態(tài)網頁。
模板可以理解為事先定義好的HTML文檔文件,模板渲染的作用機制可以簡單理解為文本替換操作—使用相應的數據去替換HTML文檔中事先準備好的標記。
模板的誕生是為了將顯示與數據分離(即前后端分離),模板技術多種多樣,但其本質是將模板文件和數據通過模板引擎生成最終的HTML代碼。
模板引擎很多,Python的jinja,nodejs的jade等都很好。
二、go語言模板引擎
Go語言內置了文本模板引擎text/template和用于HTML文檔的html/template。它們的作用機制可以簡單歸納如下:
- 模板文件通常定義為
.tmpl和.tpl為后綴(也可以使用其他的后綴),必須使用UTF8編碼。 - 模板文件中使用
{{和}}包裹和標識需要傳入的數據。 - 傳給模板的數據可以通過點號
.來訪問,如果數據是復雜類型的數據,可以通過{{ .FieldName }}來訪問它的字段。 - 除
{{和}}包裹的內容外,其他內容均不做修改原樣輸出。
三、模板引擎的使用
Go語言模板引擎的使用可以分為三部分:定義模板文件、解析模板文件和模板渲染。
1、定義模板
按照相應的語法規(guī)則去編寫模板
2、解析模板
template包提供了以下方法解析模板,獲得模板對象
// 創(chuàng)建模板對象,并為其添加一個模板名稱
func New(name string) *Template {}
// 解析字符串
// 可以使用template.New("name").Parse(src string)
// 來創(chuàng)建模板對象,并完成解析模板內容。
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在創(chuàng)建時就為其添加一個模板名稱,并且執(zhí)行t.Execute()會默認去尋找該名稱進行數據融合。
使用ParseFiles一次指定多個文件加載多個模板進來,就不可以使用t.Execute()來執(zhí)行數據融合,可以通過t.ExecuteTemplate()方法指定模板名稱來執(zhí)行數據融合。
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 程序差不多,也是條件語句、循環(huán)語句、變量、函數調用等等…
1、注釋
{{/* a comment */}}
// 注釋,執(zhí)行時會忽略。可以多行。注釋不能嵌套,并且必須緊貼分界符始止。
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包創(chuàng)建新的模板的時候,支持.Funcs方法來將自定義的函數集合導入到該模板中,后續(xù)通過該模板渲染的文件均支持直接調用這些函數。
該函數集合的定義為:
type FuncMap map[string]interface{}
key為方法的名字,value則為函數。這里函數的參數個數沒有限制,但是對于返回值有所限制。有兩種選擇,一種是只有一個返回值,還有一種是有兩個返回值,但是第二個返回值必須是error類型的。這兩種函數的區(qū)別是第二個函數在模板中被調用的時候,假設模板函數的第二個參數的返回不為空,則該渲染步驟將會被打斷并報錯。
- 內置模板函數:
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、循環(huán)遍歷
{{ range pipeline }} T1 {{ end }}
// 如果 pipeline 的長度為 0 則輸出 else 中的內容
{{ range pipeline }} T1 {{ else }} T2 {{ end }}
range可以遍歷slice、數組、map或channel。遍歷的時候,會設置.為當前正在遍歷的元素。
對于第一個表達式,當遍歷對象的值為0值時,則range直接跳過,就像if一樣。對于第二個表達式,則在遍歷到0值時執(zhí)行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值時,執(zhí)行else語句塊T0,否則.設置為pipeline運算的值,并執(zhí)行T1。
9、模板嵌套
- define
define可以直接在待解析內容中定義一個模板
// 定義名稱為name的template
{{ define "name" }} T {{ end }}
- template
使用template來執(zhí)行模板
// 執(zhí)行名為name的template
{{ template "name" }}
{{ template "name" pipeline }}
- block
{{ block "name" pipeline }} T {{ end }}
block等價于define定義一個名為name的模板,并在"有需要"的地方執(zhí)行這個模板,執(zhí)行時將.設置為pipeline的值。
等價于:先 {{ define "name" }} T {{ end }} 再執(zhí)行 {{ 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沒有該功能。
上下文感知具體指的是根據所處環(huán)境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)
}
運行程序,頁面顯示:
<script>alert("helloworld")</script>
不轉義
上下文感知的自動轉義能讓程序更加安全,比如防止XSS攻擊(例如在表單中輸入帶有<script>...</script>的內容并提交,會使得用戶提交的這部分script被執(zhí)行)。
如果確實不想轉義,可以進行類型轉換。
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 >baidu</a>`,
}
t.Execute(w, m)
}
運行程序,頁面顯示:
<script>alert("helloworld")</script>
baidu