在部分中,变量不可见。

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

Variable not visible in partial

问题

为什么我的page对象的title属性在头部模板中没有填充?

这是我的一个小型Go程序:

  1. package main
  2. import (
  3. "html/template"
  4. "os"
  5. )
  6. type Page struct {
  7. Title string
  8. Body string
  9. }
  10. func main() {
  11. f, _ := os.Create("index.html")
  12. defer f.Close()
  13. page := Page{"I'm the title", "And I'm the body"}
  14. t := template.New("post.html")
  15. t = template.Must(t.ParseGlob("templates/*html"))
  16. t.Execute(f, page)
  17. }

这是templates/post.html文件的内容:

  1. {{template "header.html"}}
  2. <article>
  3. {{.Body}}
  4. </article>
  5. {{template "footer.html"}}

这是templates/header.html文件的内容:

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>{{.Title}}</title>
  6. </head>
  7. <body>

为了完整起见,这是templates/footer.html文件的内容:

  1. <footer>
  2. &copy; 2015
  3. </footer>
  4. </body>
  5. </html>

模板templates/post.html中的.Body变量被填充了,但是模板templates/header.html中的.Title为空,我认为这是因为它是从另一个模板渲染的局部模板...

如何使其工作?

我将完整的示例发布在gist上。

英文:

Why is the title attribute of my page object not populated in the header template ?

here is my small go program

  1. package main
  2. import (
  3. &quot;html/template&quot;
  4. &quot;os&quot;
  5. )
  6. type Page struct {
  7. Title string
  8. Body string
  9. }
  10. func main() {
  11. f, _ := os.Create(&quot;index.html&quot;)
  12. defer f.Close()
  13. page := Page{&quot;I&#39;m the title&quot;, &quot;And I&#39;m the body&quot;}
  14. t := template.New(&quot;post.html&quot;)
  15. t = template.Must(t.ParseGlob(&quot;templates/*html&quot;))
  16. t.Execute(f, page)
  17. }

here is the templates/post.html file:

  1. {{template &quot;header.html&quot;}}
  2. &lt;article&gt;
  3. {{.Body}}
  4. &lt;/article&gt;
  5. {{template &quot;footer.html&quot;}}

and my templates/header.html file:

  1. &lt;!doctype html&gt;
  2. &lt;html&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;utf-8&quot;&gt;
  5. &lt;title&gt;{{.Title}}&lt;/title&gt;
  6. &lt;/head&gt;
  7. &lt;body&gt;

for the sake of completeness, my footer, templates/footer.html file:

  1. &lt;footer&gt;
  2. &amp;copy; 2015
  3. &lt;/footer&gt;
  4. &lt;/body&gt;
  5. &lt;/html&gt;

The .Body variable in the templates/post.html template is filled, but the .Title in the templates/header.html template is empty, I think it's because it's a partial, rendered from another template...

How to make this work ?

I posted the complete above example as a gist

答案1

得分: 5

{{template "header.html"}} 表单没有传递任何数据。

尝试使用 {{template "header.html" .}} 替代。

有关详细信息,请参阅 text/template 的“Actions”部分。

英文:

The {{template &quot;header.html&quot;}} form doesn't pass any data.

Try {{template &quot;header.html&quot; .}} instead.

See the Actions section of text/template for details.

huangapple
  • 本文由 发表于 2015年4月18日 20:50:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/29717586.html
匿名

发表评论

匿名网友

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

确定