嵌套模板未被渲染:Golang

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

Nested template is not being rendered : Golang

问题

Perl中的应用程序通过SSI调用Golang中的标头来渲染横幅。在Golang中,标头模板{gold_shop_header.shtml}包含另一个模板{reputation_level_info.html}。问题是包含模板的内容没有被渲染。

参考代码:

gold_shop_header.shtml

{{define "Gold Banner"}}
.
.


{{ Loc .Lang "Reputation" }}
{{ template "reputation_level" . }}

.
.
{{ end }}

reputation_level_info.html

{{define "reputation_level"}}

{{ Loc .Lang "Reputation Levels" }}
  1. <table class=" mt-10 fs-8 table-repsys" >
  2. <tbody>
  3. <tr class="bold">
  4. <th>{{ Loc .Lang "Badges" }}</th>
  5. <th>{{ Loc .Lang "Name" }}</th>
  6. <th>{{ Loc .Lang "Points" }}</th>
  7. </tr>
  8. {{ range .RepLevels }}
  9. <tr class="{{ .Class}}">
  10. <td>
  11. <span class="badges-lib {{ .ImageName}}"></span>
  12. </td>
  13. {{ if eq .Badges "off" }}
  14. <td>
  15. {{ .Badges }}
  16. </td>
  17. <td>
  18. {{ if .UpperBound }}
  19. {{ .LowerBound }} - {{ .UpperBound }} Points
  20. {{ else }}
  21. &gt; {{ .LowerBound }} Points
  22. {{ end }}
  23. </td>
  24. {{ else }}
  25. <td colspan="2">{{ Loc ..Lang "Reputation Off" }}</td>
  26. {{ end }}
  27. </tr>
  28. {{ end }}
  29. </tbody>
  30. </table>
  31. </div>
  32. </div>


{{ end }}

gen.go

var reputationTemplatePath = "/var/ssi/banner/gold/reputation_level_info.html"

_goldShopReputationTemplateBytes, err := ioutil.ReadFile(reputationTemplatePath)
if err != nil {
log.Panic("err", "error reading template", err)
}

var goldShopRepString = string(_goldShopReputationTemplateBytes)

var headerTemplatePath = "/var/ssi/banner/gold/gold_shop_header.shtml"

_goldShopHeaderTemplateBytes, err := ioutil.ReadFile(headerTemplatePath)
if err != nil {
log.Panic("err", "error reading template", err)
}

var goldShopHeaderString = string(_goldShopHeaderTemplateBytes)

var templatesString [] string

GoldShopHeaderTemplate, err = template.New("Gold Shop Header").Funcs(GetTemplateFunctionMap()).
Parse(goldShopHeaderString)
if err != nil {
log.Panic("err", "error parsing template", err)
}
GoldShopHeaderTemplate.Parse(goldShopRepString)

htmlBuffer := bytes.NewBufferString("")
template_exec_err := GoldShopHeaderTemplate.Execute(htmlBuffer, argsHeader)

请指出问题出在哪里。我对Golang不熟悉。包含的模板没有被渲染。另外,如果我删除{{ define "Gold Banner" }}语句,主模板会被渲染吗?如果包含这个语句,什么都不会被渲染。没有报错信息。

英文:

The application in perl calls the header in golang via SSI to render the banner.
In golang the header template {gold_shop_header.shtml} inculdes another template {reputation_level_info.html}. The issue is that the contents of the included template are not being rendered.

Code for reference:

  1. gold_shop_header.shtml
  2. {{define &quot;Gold Banner&quot;}}
  3. .
  4. .
  5. &lt;div class=&quot;text-center mt-3&quot;&gt;
  6. &lt;span class=&quot;fs-12 font-default cursor-default&quot;&gt;
  7. {{ Loc .Lang &quot;Reputation&quot; }}
  8. {{ template &quot;reputation_level&quot; . }}
  9. &lt;/span&gt;
  10. &lt;/div&gt;
  11. .
  12. .
  13. {{ end }}
  14. reputation_level_info.html
  15. {{define &quot;reputation_level&quot;}}
  16. &lt;i class=&quot;icon-help-alt ml-5&quot;&gt;
  17. &lt;div class=&quot;absolute text-left&quot; style=&quot;&quot;&gt;
  18. &lt;div class=&quot;relative hover-reputation-lib&quot;&gt;
  19. &lt;div class=&quot;relative w-100p&quot;&gt;
  20. &lt;div class=&quot;fs-18 mt-5&quot;&gt;{{ Loc .Lang &quot;Reputation Levels&quot; }}&lt;/div&gt;
  21. &lt;table class=&quot; mt-10 fs-8 table-repsys&quot; &gt;
  22. &lt;tbody&gt;
  23. &lt;tr class=&quot;bold&quot;&gt;
  24. &lt;th&gt;{{ Loc .Lang &quot;Badges&quot; }}&lt;/th&gt;
  25. &lt;th&gt;{{ Loc .Lang &quot;Name&quot; }}&lt;/th&gt;
  26. &lt;th&gt;{{ Loc .Lang &quot;Points&quot; }}&lt;/th&gt;
  27. &lt;/tr&gt;
  28. {{ range .RepLevels }}
  29. &lt;tr class=&quot;{{ .Class}}&quot;&gt;
  30. &lt;td&gt;
  31. &lt;span class=&quot;badges-lib {{ .ImageName}}&quot;&gt;&lt;/span&gt;
  32. &lt;/td&gt;
  33. {{ if eq .Badges &quot;off&quot; }}
  34. &lt;td&gt;
  35. {{ .Badges }}
  36. &lt;/td&gt;
  37. &lt;td&gt;
  38. {{ if .UpperBound }}
  39. {{ .LowerBound }} - {{ .UpperBound }} Points
  40. {{ else }}
  41. &amp;gt; {{ .LowerBound }} Points
  42. {{ end }}
  43. &lt;/td&gt;
  44. {{ else }}
  45. &lt;td colspan=&quot;2&quot;&gt;{{ Loc ..Lang &quot;Reputation Off&quot; }}&lt;/td&gt;
  46. {{ end }}
  47. &lt;/tr&gt;
  48. {{ end }}
  49. &lt;/tbody&gt;
  50. &lt;/table&gt;
  51. &lt;/div&gt;
  52. &lt;/div&gt;
  53. &lt;/div&gt;
  54. &lt;/i&gt;
  55. {{ end }}
  56. gen.go
  57. var reputationTemplatePath = &quot;/var/ssi/banner/gold/reputation_level_info.html&quot;
  58. _goldShopReputationTemplateBytes, err := ioutil.ReadFile(reputationTemplatePath)
  59. if err != nil {
  60. log.Panic(&quot;err&quot;, &quot;error reading template&quot;, err)
  61. }
  62. var goldShopRepString = string(_goldShopReputationTemplateBytes)
  63. var headerTemplatePath = &quot;/var/ssi/banner/gold/gold_shop_header.shtml&quot;
  64. _goldShopHeaderTemplateBytes, err := ioutil.ReadFile(headerTemplatePath)
  65. if err != nil {
  66. log.Panic(&quot;err&quot;, &quot;error reading template&quot;, err)
  67. }
  68. var goldShopHeaderString = string(_goldShopHeaderTemplateBytes)
  69. var templatesString [] string
  70. GoldShopHeaderTemplate, err = template.New(&quot;Gold Shop Header&quot;).Funcs(GetTemplateFunctionMap()).
  71. Parse(goldShopHeaderString)
  72. if err != nil {
  73. log.Panic(&quot;err&quot;, &quot;error parsing template&quot;, err)
  74. }
  75. GoldShopHeaderTemplate.Parse(goldShopRepString)
  76. htmlBuffer := bytes.NewBufferString(&quot;&quot;)
  77. template_exec_err := GoldShopHeaderTemplate.Execute(htmlBuffer, argsHeader)

Please suggest what is wrong. I am new to golang. The included template is not being rendered. also, for the main template if I remove the {{ define " Gold Banner"}} statement does it get rendered, if I include this statement nothing gets rendered.
No error is reported.

答案1

得分: 1

你忘记在解析goldShopRepString后重新分配GoldShopHeaderTemplate

这行代码:

  1. GoldShopHeaderTemplate.Parse(goldShopRepString)

应该改为:

  1. GoldShopHeaderTemplate, err := GoldShopHeaderTemplate.Parse(goldShopRepString)
英文:

You forgot to reassign the GoldShopHeaderTemplate after you parsed goldShopRepString

This line:

  1. GoldShopHeaderTemplate.Parse(goldShopRepString)

Should Be:

  1. GoldShopHeaderTemplate, err := GoldShopHeaderTemplate.Parse(goldShopRepString)

huangapple
  • 本文由 发表于 2016年11月30日 10:58:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/40879481.html
匿名

发表评论

匿名网友

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

确定