Go Web页面模板返回空白。

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

Go Web Page Template served Blank

问题

我用Go语言编写了以下代码,用于通过Go程序传递模板和参数值来提供网页:

// +build

package main

import (
	"html/template"
	"io/ioutil"
	"net/http"
)

type Blog struct {
	title   string
	heading string
	para    string
}

func loadfile(filename string) (string, error) {
	byte, err := ioutil.ReadFile(filename)
	return string(byte), err
}

func handler(w http.ResponseWriter, r *http.Request) {
	blog := Blog{title: "GoLang", heading: "WebServer", para: "coding"}
	t, err := template.ParseFiles("test.html")
	fmt.Println(err)
	t.Execute(w, blog)
}

func main() {
	http.HandleFunc("/", handler)
	http.ListenAndServe(":9000", nil)
}

HTML模板文件名为test.html,内容如下:

<html>
<head>
<title>{{.title}}</title>
</head>
<body>

<h1>{{.heading}}</h1>
<p>{{.para}}</p>

</body>
</html>

当我执行该程序时,页面显示为空白。应该传递给模板的参数没有显示在渲染的页面上。我甚至打印了错误,但没有错误信息显示。

英文:

I wrote this Go Lang code for serving web page with template and values passed to parameters through Go program

// +build

package main

import (
	&quot;html/template&quot;
	&quot;io/ioutil&quot;
	&quot;net/http&quot;
)
type Blog struct {
	title   string
	heading string
	para    string
}
func loadfile(filename string) (string, error) {
	byte, err := ioutil.ReadFile(filename)
	return string(byte), err
}
func handler(w http.ResponseWriter, r *http.Request) {
	blog := Blog{title: &quot;GoLang&quot;, heading: &quot;WebServer&quot;, para: &quot;coding&quot;}
	t, err := template.ParseFiles(&quot;test.html&quot;)
    fmt.Println(err)
	t.Execute(w, blog)
}
func main() {
	http.HandleFunc(&quot;/&quot;, handler)
	http.ListenAndServe(&quot;:9000&quot;, nil)
}

HTML Template file named as test.html as follows

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;{{.title}}&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;h1&gt;{{.heading}}&lt;/h1&gt;
&lt;p&gt;{{.para}}&lt;/p&gt;

&lt;/body&gt;
&lt;/html&gt; 

When I execute the program, the issue is that the page served is coming up as blank. The parameters that were to be passed to the template don't show up on the page rendered. I even printed the error but there is no error

答案1

得分: 2

你应该将Blog结构体的字段的首字母大写,以使它们变为公共字段。

type Blog struct {
    Title   string
    Heading string
    Para    string
}

此外,你可以将一个map传递给Execute()方法,就像这样:

b := map[string]string{
    "title":   "GoLang",
    "heading": "WebServer",
    "para":    "coding",
}
t.Execute(w, b)
英文:

You should write the first letter of the fields of Blog with upper case to make them public

type Blog struct {
    Title   string
    Heading string
    Para    string
}

Also you can pass a map to the method Execute(), something like this:

b := map[string]string{
    &quot;title&quot;: &quot;GoLang&quot;,
    &quot;heading&quot;: &quot;WebServer&quot;,
    &quot;para&quot;: &quot;coding&quot;,
}
t.Execute(w, b)

huangapple
  • 本文由 发表于 2016年12月11日 18:21:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/41085135.html
匿名

发表评论

匿名网友

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

确定