模板语言:
1.if
• not ⾮
{{if not .condition}}
{{end}}
• and 与
{{if and .condition1 .condition2}}
{{end}}
• or 或
{{if or .condition1 .condition2}}
{{end}}
• eq 等于
{{if eq .var1 .var2}}
{{end}}
• ne 不等于
{{if ne .var1 .var2}}
{{end}}
• lt ⼩于 (less than)
{{if lt .var1 .var2}}
{{end}}
• le ⼩于等于
{{if le .var1 .var2}}
{{end}}
• gt ⼤于
{{if gt .var1 .var2}}
{{end}}
• ge ⼤于等于
{{if ge .var1 .var2}}
{{end}}
2.{{.}}
<html>
<head>
</head>
<body>
<p>hello, old man, {{.}}</p>
</body>
</html>
3.{{with .Var}}
<html> <head>
</head>
<body>
{{with .Name}}
<p>hello, old man, {{.}}</p>
{{end}}
</body>
</html>
4.循环
<html>
<head>
</head>
<body>
{{range .}}
{{if gt .Age 18}}
<p>hello, old man, {{.Name}}</p>
{{else}}
<p>hello,young man, {{.Name}}</p>
{{end}}
{{end}}
</body>
</html>
. 代表了传进去的对象
例:
1.if 渲染模板到终端
package main import ( "fmt" "text/template" "os" ) type Person struct{ Name string Age int } func main(){ t,err := template.ParseFiles("index.html") //从index.html中读取内容,并生成模板。 if err != nil { fmt.Println("parse file error:",err) return } p := Person{ Name:"whj", Age:17, } if err := t.Execute(os.Stdout,p);err != nil { //把渲染的模板内容打印到终端 fmt.Println("there was an error:",err.Error) } }
index.html
<html> <head> </head> <body> {{if gt .Age 18}} <p>hello, old man, {{.Name}}</p> {{else}} <p>hello,young man, {{.Name}}</p> {{end}} </body> </html>
运行结果:
2.if 渲染模板到网页
package main import ( "fmt" "text/template" // "os" "net/http" ) type Person struct{ Name string Age int } var tem *template.Template func init(){ t,err := template.ParseFiles("index.html") //从index.html中读取内容,并生成模板,把生成模板单独提取出来,利用init函数自动执行生成模板 if err != nil { fmt.Println("parse file error:",err) return } tem = t //利用全局变量保存生成的模板,方便使用 } func user_info(w http.ResponseWriter,r *http.Request){ p := Person{ Name:"whj", Age:17, } if err := tem.Execute(w,p);err != nil { //把渲染的模板内容写到网页 fmt.Println("there was an error:",err.Error) } } func main(){ http.HandleFunc("/user_info",user_info) err:=http.ListenAndServe(":8080",nil) if err != nil { fmt.Println("http listen failed err:",err) } /* t,err := template.ParseFiles("index.html") //从index.html中读取内容,并生成模板。 if err != nil { fmt.Println("parse file error:",err) return }*/ }
查看结果:
3. {{ . }} 渲染
index.html:
<html> <head> </head> <body> {{if gt .Age 18}} <p>hello, old man, {{.}}</p> {{else}} <p>hello,young man, {{.}}</p> {{end}} </body> </html>
查看运行结果:
. 代表了传进去的对象
4. with
index.html
<html> <head> </head> <body> {{with .Name}} <p>hello, old man, {{.}}</p> //这一行中的点代表了 .Name {{end}} </body> </html>
查看结果 :
5.循环
range:
package main import( "fmt" "text/template" "net/http" ) var t1 *template.Template func init(){ t,err := template.ParseFiles("index.html") if err != nil { fmt.Println("paser file err",err) return } t1 = t } type Person struct{ Name string Age int } func FormServer(w http.ResponseWriter, r *http.Request){ // p := Person{Name:"Mary",Age:23} var stu []*Person //生成切片,循环切片 for i:=0;i<5;i++{ stu1:=&Person{ Name:string(i), Age:i*5, } stu=append(stu,stu1) //生成切片 } t1.Execute(w,stu) //把切片传入模板 } func main(){ http.HandleFunc("/userinfo",FormServer) http.ListenAndServe(":8080",nil) }
index.html:
<html> <head> </head> <body> {{range .}} {{if gt .Age 18}} <p>hello, old man, {{.Name}}</p> {{else}} <p>hello,young man, {{.Name}}</p> {{end}} {{end}} </body> <html>
查看运行结果: