快速開始
~~~
func handler(w http.ResponseWriter,r *http.Request){
t := template.New("new template") //創建一個模板
t,err := t.ParseFile("tpl/welcome.html",nil) //解析模板文件
if err != nil{
panic(err.Error())
}
user := GetUser() //獲取用戶信息
t.Execute(w,user) //執行模板,并通過w輸出
}
~~~
各種模板
{{.}} 表示當前對象,如user對象
{{.FieldName}} 表示對象的某個字段
{{range …}}{{end}} go中for…range語法類似,循環
{{with …}}{{end}} 當前對象的值,上下文
~~~
//一個模板文件例子
hello {{.UserName}}!
{{range .Emails}}
an email {{.}}
{{end}}
{{with .Friends}}
{{range .}}
my friend name is {{.Fname}}
{{end}}
{{end}}
~~~
{{if …}}{{else}}{{end}} go中的if-else語法類似,條件選擇
~~~
//if 后只能出現bool值,不支持bool表達式
if-else demo:
{{if .ShowFlag}}
section if
{{else}}
section else.
{{end}}
~~~
pipeline
左邊的輸出作為右邊的輸入
~~~
//當前對象作為參數,傳入內置函數html中
{{. | html}}
~~~
模板變量
模板使用過程中定義局部變量
~~~
{{with $x := "output"}}{{$x}}{{end}}
{{with $x := "output"}}{{$x | printf "%s"}}{{end}}
~~~
輸出
~~~
output
output
~~~
模板函數
支持的函數類型
~~~
func(args ...interface{})string
~~~
函數定義例子
~~~
func Say(args ...interface{})string{
return fmt.Sprintf("%s %v","Hello",args[0])
}
~~~
注冊函數
~~~
t = t.Funcs(template.FuncMap{"Say":Say})
~~~
模板定義
~~~
{{Say `Func`}}
{{`Pipe` | Say}}
~~~
輸出:
~~~
Hello Func
Hello Pipe
~~~
template中內置函數
~~~
var builtins = FuncMap{
"and": and,
"call": call,
"html": HTMLEscaper,
"index": index,
"js": JSEscaper,
"len": length,
"not": not,
"or": or,
"print": fmt.Sprint,
"printf": fmt.Sprintf,
"println": fmt.Sprintln,
"urlquery": URLQueryEscaper,
}
~~~
Must操作
檢測模板是否正確:大括號是否匹配,注釋是否正確關閉,變量是否正確
~~~
tOk := template.New("first")
template.Must(tOk.Parse(" some static text /* and a comment */"))
fmt.Println("The first one parsed OK.")
template.Must(template.New("second").Parse("some static text {{ .Name }}"))
fmt.Println("The second one parsed OK.")
fmt.Println("The next one ought to fail.")
tErr := template.New("check parse error with Must")
template.Must(tErr.Parse(" some static text {{ .Name }"))
~~~
輸出:
~~~
The first one parsed OK.
The second one parsed OK.
The next one ought to fail.
panic: template: check parse error with Must:1: unexpected "}" in command
~~~
模板嵌套
將模板模塊化,如在web開發中定義header,content,footer三個子模塊
~~~
//聲明
{{define "module_name"}}content{{end}}
//調用
{{template "module_name"}}
~~~
以下定義三個文件:
- header.html
~~~
{{define "header"}}
<html>
<head>
<title>演示信息</title>
</head>
<body>
{{end}}
~~~
footer.tmpl
~~~
{{define "footer"}}
</body>
</html>
{{end}}
~~~
content.tmpl
~~~
{{define "content"}}
{{template "header"}}
<h1>演示嵌套</h1>
<ul>
<li>嵌套使用define定義子模板</li>
<li>調用使用template</li>
</ul>
{{template "footer"}}
{{end}}
~~~
go代碼
ParseFiles:所有嵌套的模板全部解析到模板中
~~~
s1,_ := template.ParseFiles("header.tmpl","content.tmpl","footer.tmpl")
s1.ExecuteTemplate(w, "header",nil)
s1.ExecuteTemplate(w, "footer", nil)
s1.ExecuteTemplate(w, "content", nil)
~~~

- Go語言基礎篇
- Go語言簡介
- Go語言教程
- Go語言環境安裝
- Go語言結構
- Go語言基礎語法
- Go語言數據類型
- Go語言變量
- Go語言提高篇
- Go語言實現貪吃蛇
- Go 諺語
- 解決連通性問題的四種算法
- golang 幾種字符串的連接方式
- Go JSON 技巧
- Go += 包版本
- Golang 編譯成 DLL 文件
- Go指南:牛頓法開方
- Go語言異步服務器框架原理和實現
- Golang適合高并發場景的原因分析
- 如何設計并實現一個線程安全的 Map ?(上篇)
- go語言執行cmd命令關機、重啟等
- IT雜項
- IT 工程師的自我管理
- IT界不為人知的14個狗血故事
- Go語言版本說明
- Go 1.10中值得關注的幾個變化
- Golang面試題解析
- Golang面試題
- Golang語言web開發
- golang 模板(template)的常用基本語法
- go語言快速入門:template模板
- Go Template學習筆記
- LollipopGo框架
- 框架簡介
- Golang語言版本設計模式
- 設計模式-單例模式
- Golang語言資源下載
- 公眾賬號
- leaf
- 合作講師
- 公開課目錄