英文:
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"}}
<table class=" mt-10 fs-8 table-repsys" >
<tbody>
<tr class="bold">
<th>{{ Loc .Lang "Badges" }}</th>
<th>{{ Loc .Lang "Name" }}</th>
<th>{{ Loc .Lang "Points" }}</th>
</tr>
{{ range .RepLevels }}
<tr class="{{ .Class}}">
<td>
<span class="badges-lib {{ .ImageName}}"></span>
</td>
{{ if eq .Badges "off" }}
<td>
{{ .Badges }}
</td>
<td>
{{ if .UpperBound }}
{{ .LowerBound }} - {{ .UpperBound }} Points
{{ else }}
> {{ .LowerBound }} Points
{{ end }}
</td>
{{ else }}
<td colspan="2">{{ Loc ..Lang "Reputation Off" }}</td>
{{ end }}
</tr>
{{ end }}
</tbody>
</table>
</div>
</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:
gold_shop_header.shtml
{{define "Gold Banner"}}
.
.
<div class="text-center mt-3">
<span class="fs-12 font-default cursor-default">
{{ Loc .Lang "Reputation" }}
{{ template "reputation_level" . }}
</span>
</div>
.
.
{{ end }}
reputation_level_info.html
{{define "reputation_level"}}
<i class="icon-help-alt ml-5">
<div class="absolute text-left" style="">
<div class="relative hover-reputation-lib">
<div class="relative w-100p">
<div class="fs-18 mt-5">{{ Loc .Lang "Reputation Levels" }}</div>
<table class=" mt-10 fs-8 table-repsys" >
<tbody>
<tr class="bold">
<th>{{ Loc .Lang "Badges" }}</th>
<th>{{ Loc .Lang "Name" }}</th>
<th>{{ Loc .Lang "Points" }}</th>
</tr>
{{ range .RepLevels }}
<tr class="{{ .Class}}">
<td>
<span class="badges-lib {{ .ImageName}}"></span>
</td>
{{ if eq .Badges "off" }}
<td>
{{ .Badges }}
</td>
<td>
{{ if .UpperBound }}
{{ .LowerBound }} - {{ .UpperBound }} Points
{{ else }}
&gt; {{ .LowerBound }} Points
{{ end }}
</td>
{{ else }}
<td colspan="2">{{ Loc ..Lang "Reputation Off" }}</td>
{{ end }}
</tr>
{{ end }}
</tbody>
</table>
</div>
</div>
</div>
</i>
{{ 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)
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
。
这行代码:
GoldShopHeaderTemplate.Parse(goldShopRepString)
应该改为:
GoldShopHeaderTemplate, err := GoldShopHeaderTemplate.Parse(goldShopRepString)
英文:
You forgot to reassign the GoldShopHeaderTemplate
after you parsed goldShopRepString
This line:
GoldShopHeaderTemplate.Parse(goldShopRepString)
Should Be:
GoldShopHeaderTemplate, err := GoldShopHeaderTemplate.Parse(goldShopRepString)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论