html 如果添加 {{ end }},文件将无法工作
html file cannot work if {{ end }} is added
我正在使用 golang 将内容添加到 html 模板文件。主文件很简单
func main() {
server := http.Server{
Addr: "localhost:8080",
}
http.HandleFunc("/process", processCover)
server.ListenAndServe()
}
func processCover(w http.ResponseWriter, r *http.Request) {
t1, _ := template.ParseFiles("t1.html", "t2.html")
t1.Execute(w, "hello World")
}
在html文件中,如果我添加如下代码
<!-- {{ range . }}
<li>{{ . }}</li>
{{ end }} -->
后面的项目都不见了。我不明白,因为我认为这是评论。这应该是一些我不知道的关于 go web 的要点。
HTML 如果输出的上下文是 HTML,则不会呈现模板中的注释。由于您将 {{range}}
放在 HTML 注释中(在 <!--
和 -->
之间)并且由于输出是 HTML 文档,因此它被删除了。
Template.Parse()
的文档中暗示了这一点:
A template definition with a body containing only white space and comments is considered empty and will not replace an existing template's body.
查看相关内容:
我正在使用 golang 将内容添加到 html 模板文件。主文件很简单
func main() {
server := http.Server{
Addr: "localhost:8080",
}
http.HandleFunc("/process", processCover)
server.ListenAndServe()
}
func processCover(w http.ResponseWriter, r *http.Request) {
t1, _ := template.ParseFiles("t1.html", "t2.html")
t1.Execute(w, "hello World")
}
在html文件中,如果我添加如下代码
<!-- {{ range . }}
<li>{{ . }}</li>
{{ end }} -->
后面的项目都不见了。我不明白,因为我认为这是评论。这应该是一些我不知道的关于 go web 的要点。
HTML 如果输出的上下文是 HTML,则不会呈现模板中的注释。由于您将 {{range}}
放在 HTML 注释中(在 <!--
和 -->
之间)并且由于输出是 HTML 文档,因此它被删除了。
Template.Parse()
的文档中暗示了这一点:
A template definition with a body containing only white space and comments is considered empty and will not replace an existing template's body.
查看相关内容: