HTML模板返回文本而不是HTML。

huangapple go评论93阅读模式
英文:

html template returns text instead of html

问题

我有以下代码:

package main

import (
	"fmt"
	htmlTempl "html/template"
	"log"
	"net/http"
)

var templatesHtml *htmlTempl.Template
var err error

func init() {
	fmt.Println("Starting up.")
	templatesHtml = htmlTempl.Must(htmlTempl.ParseGlob("templates/*.html"))
}

func test(w http.ResponseWriter, r *http.Request) {
	err = templatesHtml.ExecuteTemplate(w, "other.html", nil)
	if err != nil {
		log.Fatalln(err)
	}
}

func main() {
	server := http.Server{
		Addr: "127.0.0.1:8080",
	}
	http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./public"))))
	http.HandleFunc("/test", test)
	server.ListenAndServe()
}

我的模板是:

// base.html的内容:
{{define "base"}}
<html>
  <head>{{template "head" .}}</head>
  <body>{{template "body" .}}</body>
</html>
{{end}}

// other.html的内容:
{{template "base" .}}
{{define "head"}}<title>other</title>{{end}}
{{define "body"}}other{{end}}

我在http://127.0.0.1:8080/test得到的输出是:

HTML模板返回文本而不是HTML。

而我期望显示正常的HTML页面!

英文:

I've the below code:

package main

import (
	&quot;fmt&quot;
	htmlTempl &quot;html/template&quot;
	&quot;log&quot;
	&quot;net/http&quot;
)

var templatesHtml *htmlTempl.Template
var err error

func init() {
	fmt.Println(&quot;Starting up.&quot;)
	templatesHtml = htmlTempl.Must(htmlTempl.ParseGlob(&quot;templates/*.html&quot;))
}

func test(w http.ResponseWriter, r *http.Request) {
	err = templatesHtml.ExecuteTemplate(w, &quot;other.html&quot;, nil)
	if err != nil {
		log.Fatalln(err)
	}
}

func main() {
	server := http.Server{
		Addr: &quot;127.0.0.1:8080&quot;,
	}
	http.Handle(&quot;/static/&quot;, http.StripPrefix(&quot;/static/&quot;, http.FileServer(http.Dir(&quot;./public&quot;))))
	http.HandleFunc(&quot;/test&quot;, test)
	server.ListenAndServe()
}

And my templates are:

// Content of base.html:
{{define &quot;base&quot;}}
&lt;html&gt;
  &lt;head&gt;{{template &quot;head&quot; .}}&lt;/head&gt;
  &lt;body&gt;{{template &quot;body&quot; .}}&lt;/body&gt;
&lt;/html&gt;
{{end}}

And

// Content of other.html:
{{template &quot;base&quot; .}}
{{define &quot;head&quot;}}&lt;title&gt;other&lt;/title&gt;{{end}}
{{define &quot;body&quot;}}other{{end}}

The output I got at http://127.0.0.1:8080/test is:

HTML模板返回文本而不是HTML。

While I was expecting normal HTML page to be displayed!

答案1

得分: 0

感谢mkoprivs提供的评论,错误在于使用//作为注释标记,通过将其替换为HTML内容注释标记来解决了该问题,如下所示:

&lt;!-- Content of other.html: --&gt;
{{template &quot;base&quot; .}}
{{define &quot;head&quot;}}&lt;title&gt;other&lt;/title&gt;{{end}}
{{define &quot;body&quot;}}other{{end}}

现在一切都正常工作了。

英文:

Thanks to the comments provided by mkoprivs the error was in using // as comment markers, it had be solved by replacing them with the html content comment marker as below:

&lt;!-- Content of other.html: --&gt;
{{template &quot;base&quot; .}}
{{define &quot;head&quot;}}&lt;title&gt;other&lt;/title&gt;{{end}}
{{define &quot;body&quot;}}other{{end}}

And it is working fine now.

huangapple
  • 本文由 发表于 2021年10月21日 16:51:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/69658789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定