综合练习示例:
package main
import (
“html/template”
“log”
“net/http”
)
func main() {
localTemplatePtr := loadTemplate()
http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
urlStr := r.URL.Path
urlStr = urlStr[1:]
destTem := localTemplatePtr.Lookup(urlStr)
if destTem != nil {
err := destTem.Execute(rw, nil)
if err != nil {
log.Fatalln(err.Error())
}
} else {
rw.WriteHeader(http.StatusNotFound)
}
})
http.Handle("/css/", http.FileServer(http.Dir(“wwwroot”)))
http.Handle("/img/", http.FileServer(http.Dir(“wwwroot”)))
http.ListenAndServe(“localhost:8010”, nil)
}
//加载模板
func loadTemplate() template.Template {
result := template.New("")
result = template.Must(result.ParseGlob("…/templates/.html"))
return result
}
模板中的Action
action 就是Go模本中迁入的命令,位于两组花括号之间的。就是一个action 而且是一个重要的一个,它代表传入模板的数据。
Action 主要可以分为五类
条件类
{{if Arg}}
some content
{{else}}
other content
{{end}}
示例:
package main
import (
“html/template”
“math/rand”
“net/http”
“time”
)
func main() {
http.HandleFunc("/proc", func(rw http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles(“home.html”)
if err == nil {
//1. 设置随机数的种子
rand.Seed(time.Now().UnixNano())
randNu := rand.Intn(10)
t.Execute(rw, randNu > 5)
} else {
rw.WriteHeader(http.StatusNotFound)
}
})
http.ListenAndServe(“localhost:8010”, nil)
}
网页:
执行结果为真
{{else}}条件结果为假
{{end}} <h1>表达式结果:{{.}}</h1>
迭代/遍历类
{{range array}}
Dot is set the element {{.}}
{{else}}
do some things
{{end}}
这类Action 用来遍历数组 ,slice map 或者 channel 等数据结构
. 用来表示每次循环遍历中的实体
回落机制: 如果传入的arry 为空 则会执行else 后边的代码
示例;
package main
import (
“net/http”
“text/template”
)
func main() {
http.HandleFunc("/range", webRangeHandle)
http.ListenAndServe(“localhost:8010”, nil)
}
//遍历handle
func webRangeHandle(rw http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles(“home.html”)
if err != nil {
rw.WriteHeader(http.StatusNotFound)
} else {
var strSlice []string
for _, v := range []rune(“发送开了房间爱上了房价哦”) {
strSlice = append(strSlice, string(v))
}
t.Execute(rw, strSlice)
}
}
页面示例:
{{.}}
{{end}} 设置类
{{with arg }}
Dot is Set to Arg
{{end}}
它准许在指定范围内 让 “.” 来表示其他的指定值(arg)它也有回落机制
示例:
package main
import (
“net/http”
“text/template”
)
func main() {
http.HandleFunc("/range", webRangeHandle)
http.ListenAndServe(“localhost:8010”, nil)
}
//遍历handle
func webRangeHandle(rw http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles(“home.html”)
if err != nil {
rw.WriteHeader(http.StatusNotFound)
} else {
var strSlice []string
for _, v := range []rune(“发送开了房间爱上了房价哦”) {
strSlice = append(strSlice, string(v))
}
t.Execute(rw, strSlice)
}
}
页面代码;
{{.}}
{{.}}
包含类
形式:{{template “name”}} 要加载所有要使用的模板
例子:
package main
import (
“net/http”
“text/template”
)
func main() {
http.HandleFunc("/range", webRangeHandle)
http.ListenAndServe(“localhost:8010”, nil)
}
//遍历handle
func webRangeHandle(rw http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles(“home.html”, “h1.html”)
if err != nil {
rw.WriteHeader(http.StatusNotFound)
} else {
var strSlice []string
for _, v := range []rune(“发送开了房间爱上了房价哦”) {
strSlice = append(strSlice, string(v))
}
t.Execute(rw, strSlice)
}
}
网页内容;
home 页面
{{.}}111 {{ template "h1.html" .}} 222
网页2 : (可以去掉头 只保留div也可以)
{{.}}
{{else}} 没有内容传入 {{end}}这里是 H1 网页内容
1.2 模板和管道
1.1 参数(argument)
参数就是模板里边用到的值:可以是bool 整数 string ,也可以是 struct struct的字段, 数组的key 等等
参数亦可以是变量 方法(返回单个值或返回一个值和一个错误) 或者函数
参数可以是一个点 “.” 也就是传入模板引擎的那个值
形式:
{{if arg}}
some content
{{end}}
这里arg就是参数
在action中设置变量
可以在action 中设置变量,变量以$开头
$variabe := value
一个迭代的action例子
{{range $key , $val :=.}}
The key is {{$key}} and the value is {{$val}}
{{end}}
1.2 管道(pipeline)
管道是按顺序连接到一起的参数 函数 和方法
和Unix 的管道类似
例如:{{p1|P2|p3}}
管道准许我们把参数的输出发给下一个参数, 下一个参数有管道(|) 分割开。
函数; 参数可以是一个函数, go模板引擎提供了一些基本的内置函数,功能比较有效,例如: fmt.Sprint 的各类变体等
开发者可以自定义函数:
可以接收到任意数量的输入参数, 返回:一个值,一个错误
内置函数: define template block
html js urlquery 对字符串进行转义,防止安全问题。
index 跟一个集合 一个索引 表示把集合中的指定索引值拿出来
print printf println