如何在GO语言和Google App Engine中解析HTML模板

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

How to parse HTML template in GO, Google App Engine

问题

我正在尝试使用Google App Engine中的GO语言制作一个页面,该页面可以使用HTML解析来显示数组的内容。

package hello

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

func init() {
	http.HandleFunc("/", root)
}

const TemplateHTML = `
<html>
  <body>
    <table width="700"  border="1" align="center">
      <tr>
        <td>
          {{range}} {{.name}} {{end}} {{range}} {{.Count}} {{end}}
        </td>
      </tr>
    </table>
  </body>
</html>
`

func root(w http.ResponseWriter, r *http.Request) {
	type variables struct {
		Name 	string
		Count   int
	}
	var data = []variables{
		{"John", 25},
		{"George", 35},
		{"NoName", 27},
	}

	//name := variables{"Somebody", 25}
	tmpl, err := template.New("i").Parse(TemplateHTML)

	if err != nil {
		fmt.Fprint(w, err)
	}
	err = tmpl.Execute(w, data)
	if err != nil {
		fmt.Fprint(w, err)
	}
}

但是我只得到了一个“内部服务器错误”。

当我使用err = tmpl.Execute(w, name)(你可以在代码的注释中找到“name”)时,一切正常。

你有任何想法是什么问题吗?我是GO语言的新手。

谢谢!

英文:

I'm trying to make a page in GO with Google App Engine which can show the content of an array with HTML parsing.

    package hello

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

func init() {
	http.HandleFunc(&quot;/&quot;, root)
}

const TemplateHTML = `
&lt;html&gt;
  &lt;body&gt;
   &lt;table width=&quot;700&quot;  border=&quot;1&quot; align=&quot;center&quot;&gt;
  &lt;tr&gt;
    &lt;td&gt;
	{{range}} {{.name}} {{end}} {{range}} {{.Count}} {{end}}
&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

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

func root(w http.ResponseWriter, r *http.Request) {
	type variables struct {
		Name 	string
		Count   int
	}
	var data = []variables{
		{&quot;John&quot;, 25},
		{&quot;George&quot;, 35},
		{&quot;NoName&quot;, 27},
	}
	
	//name := variables{&quot;Somebody&quot;, 25}
	tmpl, err := template.New(&quot;i&quot;).Parse(TemplateHTML)

	if err != nil {
		 fmt.Fprint(w, err)
	}
	err = tmpl.Execute(w, data)
	if err != nil {
		 fmt.Fprint(w, err)
	}

}

But I only get an Internal Server Error.

When I used err = tmpl.Execute(w, name) (you can find 'name' in the comment in the code) it was okay.

Do you have any idea what can be the problem? I'm new in golang.

Thank you!

答案1

得分: 4

var data = []variables{
variables{"John", 25},
variables{"George", 35},
variables{"NoName", 27},
}

const TemplateHTML = `


{{range .}}

{{end}}

{{.Name}} {{.Count}}



`

英文:

Someone else will respond with a much more concise/accurate answer, but one simple way you can get your above example to work is to change your data declaration to:

var data = []variables{
    variables{&quot;John&quot;, 25},
    variables{&quot;George&quot;, 35},
    variables{&quot;NoName&quot;, 27},
}

Here, you are making each element of the data slice of type variables, which will match the variables type in the definition. I believe this was the main issue with your current version - the proper data type was not being supplied to data. Then, you will just need to adjust your template so that your string looks as follows:

const TemplateHTML = `
&lt;html&gt;
  &lt;body&gt;
   &lt;table width=&quot;700&quot;  border=&quot;1&quot; align=&quot;center&quot;&gt;
    {{range .}}
    &lt;tr&gt;
      &lt;td&gt;{{.Name}}&lt;/td&gt;&lt;td&gt;{{.Count}}&lt;/td&gt;
    &lt;/tr&gt;
    {{end}}
  &lt;/tr&gt;
&lt;/table&gt;

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

The key changes here were: making .Name uppercase so it matches the property name and wrapping tr in the {{range .}} block so that a new row gets created for each entry (I assumed that's what you were looking for - if not, just make sure that range encompasses whatever dynamic content you want).

答案2

得分: 1

如果template.Parse返回一个错误,你就不能使用它返回的其他值。如果模板执行失败,你试图在一个空的tmpl上调用tmpl.Execute,这将导致恐慌。

英文:

If template.Parse returns an error, you can't use the other value it returns. You're trying to call tmpl.Execute on a nil tmpl if the template execution failed, which would lead to a panic.

huangapple
  • 本文由 发表于 2012年11月26日 03:26:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/13554598.html
匿名

发表评论

匿名网友

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

确定