gotemplate未渲染数据。

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

gotemplate not rendering data

问题

  1. func renderTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
  2. layout := "static/layout.html"
  3. tmplFiles := []string{layout, tmpl}
  4. fmt.Printf("%v\n", data)
  5. t, err := template.ParseFiles(tmplFiles...)
  6. if err != nil {
  7. http.Error(w, err.Error(), http.StatusInternalServerError)
  8. return
  9. }
  10. err = t.ExecuteTemplate(w, "layout.html", data)
  11. if err != nil {
  12. http.Error(w, err.Error(), http.StatusInternalServerError)
  13. return
  14. }
  15. }
  16. func main(){
  17. http.HandleFunc("/directors", directorsHandler)
  18. fs := http.FileServer(http.Dir("static"))
  19. http.Handle("/static/", http.StripPrefix("/static/", fs))
  20. fmt.Println("Server listening on port 8080...")
  21. http.ListenAndServe(":8080", nil)
  22. }
  23. directors.html
  24. {{ define "content" }}
  25. Directors ----
  26. <h1>{{ .Title }}</h1>
  27. <ul>
  28. {{ range .Directors }}
  29. hi
  30. <li>
  31. <p><strong>Name:</strong> {{ .Name }}</p>
  32. <p><strong>Date of Birth:</strong> {{ .DateOfBirth }}</p>
  33. <p><strong>Nationality:</strong> {{ .Nationality }}</p>
  34. </li>
  35. {{ end }}
  36. </ul>
  37. {{ end }}
  38. func directorsHandler(w http.ResponseWriter, r *http.Request) {
  39. data := struct {
  40. Title string
  41. Directors []struct {
  42. Name string
  43. DateOfBirth string
  44. Nationality string
  45. }
  46. }{
  47. Title: "Director List",
  48. Directors: []struct {
  49. Name string
  50. DateOfBirth string
  51. Nationality string
  52. }{
  53. {
  54. Name: "Christopher Nolan",
  55. DateOfBirth: "July 30, 1970",
  56. Nationality: "British",
  57. },
  58. {
  59. Name: "Quentin Tarantino",
  60. DateOfBirth: "March 27, 1963",
  61. Nationality: "American",
  62. },
  63. // Add more directors as needed
  64. },
  65. }
  66. renderTemplate(w, "static/directors.html", data)
  67. }

不知道为什么 .Title 没有被渲染出来。
我怀疑是因为接口的原因,但没有找到相关的信息。
我使用匿名结构体将不同的数据发送到不同的页面。
我尝试将信息发送到 directors.html 模板的内容,但它不会被渲染出来。
更多细节:
数据被发送到 data interface{}

英文:
  1. func renderTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
  2. layout := &quot;static/layout.html&quot;
  3. tmplFiles := []string{layout, tmpl}
  4. fmt.Printf(&quot;%v\n&quot;, data)
  5. t, err := template.ParseFiles(tmplFiles...)
  6. if err != nil {
  7. http.Error(w, err.Error(), http.StatusInternalServerError)
  8. return
  9. }
  10. err = t.ExecuteTemplate(w, &quot;layout.html&quot;, data)
  11. if err != nil {
  12. http.Error(w, err.Error(), http.StatusInternalServerError)
  13. return
  14. }
  15. }
  16. func main(){
  17. http.HandleFunc(&quot;/directors&quot;, directorsHandler)
  18. fs := http.FileServer(http.Dir(&quot;static&quot;))
  19. http.Handle(&quot;/static/&quot;, http.StripPrefix(&quot;/static/&quot;, fs))
  20. fmt.Println(&quot;Server listening on port 8080...&quot;)
  21. http.ListenAndServe(&quot;:8080&quot;, nil)
  22. }
  23. directors.html
  24. {{ define &quot;content&quot; }}
  25. Directors ----
  26. &lt;h1&gt;{{ .Title }}&lt;/h1&gt;
  27. &lt;ul&gt;
  28. {{ range .Directors }}
  29. hi
  30. &lt;li&gt;
  31. &lt;p&gt;&lt;strong&gt;Name:&lt;/strong&gt; {{ .Name }}&lt;/p&gt;
  32. &lt;p&gt;&lt;strong&gt;Date of Birth:&lt;/strong&gt; {{ .DateOfBirth }}&lt;/p&gt;
  33. &lt;p&gt;&lt;strong&gt;Nationality:&lt;/strong&gt; {{ .Nationality }}&lt;/p&gt;
  34. &lt;/li&gt;
  35. {{ end }}
  36. &lt;/ul&gt;
  37. {{ end }}
  38. func directorsHandler(w http.ResponseWriter, r *http.Request) {
  39. data := struct {
  40. Title string
  41. Directors []struct {
  42. Name string
  43. DateOfBirth string
  44. Nationality string
  45. }
  46. }{
  47. Title: &quot;Director List&quot;,
  48. Directors: []struct {
  49. Name string
  50. DateOfBirth string
  51. Nationality string
  52. }{
  53. {
  54. Name: &quot;Christopher Nolan&quot;,
  55. DateOfBirth: &quot;July 30, 1970&quot;,
  56. Nationality: &quot;British&quot;,
  57. },
  58. {
  59. Name: &quot;Quentin Tarantino&quot;,
  60. DateOfBirth: &quot;March 27, 1963&quot;,
  61. Nationality: &quot;American&quot;,
  62. },
  63. // Add more directors as needed
  64. },
  65. }
  66. renderTemplate(w, &quot;static/directors.html&quot;, data)
  67. }

don't know why .Title is not being rendered.
i have the suspicion that its because the interface but didn't find anything about it.
i am using anonymous struct to send different data to different pages.
I tried to send information to directors.html template content but it wont render.
more details:
the data is being send to data interface{} .

答案1

得分: 1

在发送数据到具有布局的页面时发现问题,你需要写成{{template "content" .}}而不是{{ template "content" }}。需要添加一个点。

英文:

Problem found when sending data to a page with layout you need to write {{template "content" .}} instead of {{ template "content" }}.
needed to add a dot.

huangapple
  • 本文由 发表于 2023年6月25日 07:19:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76548314.html
匿名

发表评论

匿名网友

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

确定