Golang模板停止渲染。

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

Golang template stop rendering

问题

我正在渲染一个响应。

在我的代码中,我传递了一个包含结构体的切片来显示一些信息。

在某个地方,我使用了一个选择(select)语句,并使用range方法渲染选项。

  1. package main
  2. import (
  3. "fmt"
  4. "html/template"
  5. "os"
  6. )
  7. func main() {
  8. lista := [3]string{"one", "two", "tree"}
  9. fmt.Println("Hello, playground")
  10. a := `<script type="text/text">{{range .}} <option></option>hi{{end}}</script> \n <script type="text/text">{{range .}} <option>hi!{{end}}</script> <script type="text/text">{{range .}} <option>hi!{{end}}</script> <script type="text/text">{{range .}} <option>hi!{{end}}</script>`
  11. tmp, _ := template.New("tmp").Parse(a)
  12. tmp.Execute(os.Stdout, lista)
  13. }

编辑

我复制了这个问题
http://play.golang.org/p/d62J3TOc1N
如果你添加</option>或任何其他闭合标签(以/开头),渲染将不会发生。

英文:

im rendering a response.

in my code I passed a Slice with structs to display some info

in one place i use a select and the options are rendered with a range method

  1. package main
  2. import (&quot;fmt&quot;
  3. &quot;html/template&quot;
  4. &quot;os&quot;
  5. )
  6. func main() {
  7. lista:=[3]string{&quot;one&quot;,&quot;two&quot;,&quot;tree&quot;}
  8. fmt.Println(&quot;Hello, playground&quot;)
  9. a:=`&lt;script type=&quot;text/text&quot;&gt;{{range .}} &lt;option&gt;&lt;/option&gt;hi{{end}}&lt;/script&gt; \n &lt;script type=&quot;text/text&quot;&gt;{{range .}} &lt;option&gt;hi!{{end}}&lt;/script&gt; &lt;script type=&quot;text/text&quot;&gt;{{range .}} &lt;option&gt;hi!{{end}}&lt;/script&gt; &lt;script type=&quot;text/text&quot;&gt;{{range .}} &lt;option&gt;hi!{{end}}&lt;/script&gt;`
  10. tmp,_:=template.New(&quot;tmp&quot;).Parse(a)
  11. tmp.Execute(os.Stdout,lista)

}

EDIT

I replicated the issue
http://play.golang.org/p/d62J3TOc1N
if you add the </option> or any other closure tag (starting with /) the render won't happened

答案1

得分: 0

将“html/template”替换为“text/template”。
你得到了预期的结果吗?

英文:

Replace "html/template" with "text/template"
Do you get the expected result?

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

发表评论

匿名网友

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

确定