main.go
package main
import (
"fmt"
"net/http"
"html/template"
)
func qiantao(w http.ResponseWriter, r *http.Request) {
t , err := template.ParseFiles("./t.tmpl", "./ul.tmpl")
if err != nil {
fmt.Println("Parse template failured, err: %v\n", err)
}
name := "Golang"
t.Execute(w, name)
}
func main() {
http.HandleFunc("/", qiantao)
err := http.ListenAndServe(":9000", nil)
if err != nil {
fmt.Println("Http server start failured, err:%v\n", err)
}
}
t,tmpl
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>嵌套template</h1>
{{/* 嵌套了另外一个单独的模板 */}}
{{template "ul.tmpl"}}
<hr>
{{/*嵌套一个define定义的模板*/}}
{{template "ol.tmpl"}}
你好,{{ . }}
</body>
</html>
{{/* 通过define定义模板 */}}
{{define "ol.tmpl"}}
<ol>444</ol>
<ol>555</ol>
<ol>666</ol>
{{end}}
ul.tmpl
<ul>
<li>111</li>
<li>222</li>
<li>333</li>
</ul>