Go:在新行上使用html/template打印切片中的每个元素。

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

Go: Print each element in slice on new line with html/template

问题

如何在新行上打印"apple"、"orange"和"pear"?

GO代码:

const titlepage = `
<html>
<h1>{{ .Title}}</h1>
<h1>{{ range $i := .Body}}{{$i}}{{end}}</h1>
</html>
`
type tp struct {
    Title string
    Body []string
}

func Read() ([]string) {
    a := []string{"apple", "orange", "pear"}
    return a
}

func main() {
    as := tp{Title: "Hello", Body: Read()}
    t := template.Must(template.New("Tele").Parse(titlepage))
    t.Execute(os.Stdout, as)
}

当前输出:

<html>
<h1>Hello</h1>
<h1>appleorangepear</h1>
</html>

Go Playground上的代码:http://play.golang.org/p/yhyfcq--MM

英文:

How do I print "apple", "orange", and "pear" on a new line?

GO:

const titlepage = `
&lt;html&gt;
&lt;h1&gt;{{ .Title}}&lt;/h1&gt;
&lt;h1&gt;{{ range $i := .Body}}{{$i}}{{end}}&lt;/h1&gt;
&lt;/html&gt;
`
type tp struct {
    Title string
    Body []string
}

func Read() ([]string) {
    a := []string{&quot;apple&quot;, &quot;orange&quot;, &quot;pear&quot;}
    return a
}

func main() {
    as := tp{Title: &quot;Hello&quot;, Body: Read()}
    t := template.Must(template.New(&quot;Tele&quot;).Parse(titlepage))
    t.Execute(os.Stdout, as)
}

Current output:

&lt;html&gt;
&lt;h1&gt;Hello&lt;/h1&gt;
&lt;h1&gt;appleorangepear&lt;/h1&gt;
&lt;/html&gt;

The code on Go Playground: http://play.golang.org/p/yhyfcq--MM

答案1

得分: 4

模板中的换行符将被复制到结果中。如果你想在{{$i}}之后换行,只需添加一个换行符。

编辑:如果你想在网页浏览器中显示换行符,你需要使用像&lt;br/&gt;这样的HTML元素,或者将你的项目放在&lt;li&gt;(列表)中。我在代码中添加了一个&lt;br/&gt;

const titlepage = `
&lt;html&gt;
&lt;h1&gt;{{ .Title}}&lt;/h1&gt;
&lt;h1&gt;{{ range $i := .Body}}{{$i}}&lt;br/&gt;
{{end}}&lt;/h1&gt;
&lt;/html&gt;
`
type tp struct {
    Title string
    Body []string
}

func Read() ([]string) {
    a := []string{"apple", "orange", "pear"}
    return a
}

func main() {
    as := tp{Title: "Hello", Body: Read()}
    t := template.Must(template.New("Tele").Parse(titlepage))
    t.Execute(os.Stdout, as)
}

你可以在这里查看代码的运行结果:http://play.golang.org/p/1G0CIfhb8a

英文:

Newline characters from the template will be copied into the result. If you want a newline after {{$i}}, you just need to add one.

Edit: If you want a newline to appear in the web browser, you need to use an HTML element like &lt;br/&gt;, or put your items in a &lt;li&gt; (list). I added a &lt;br/&gt; to the code.

http://play.golang.org/p/1G0CIfhb8a

const titlepage = `
&lt;html&gt;
&lt;h1&gt;{{ .Title}}&lt;/h1&gt;
&lt;h1&gt;{{ range $i := .Body}}{{$i}}&lt;br/&gt;
{{end}}&lt;/h1&gt;
&lt;/html&gt;
`
type tp struct {
    Title string
    Body []string
}

func Read() ([]string) {
    a := []string{&quot;apple&quot;, &quot;orange&quot;, &quot;pear&quot;}
    return a
}

func main() {
    as := tp{Title: &quot;Hello&quot;, Body: Read()}
    t := template.Must(template.New(&quot;Tele&quot;).Parse(titlepage))
    t.Execute(os.Stdout, as)
}

huangapple
  • 本文由 发表于 2014年8月16日 01:13:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/25330985.html
匿名

发表评论

匿名网友

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

确定