未呈现嵌套模板:Golang
Nested template is not being rendered : Golang
perl 中的应用程序通过 SSI 调用 golang 中的 header 来呈现横幅。
在 golang 中,header 模板 {gold_shop_header.shtml} 包含另一个模板 {reputation_level_info.html}。问题是包含的模板的内容没有被渲染。
参考代码:
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 }}
> {{ .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)
请指出错误之处。我是戈朗的新手。未呈现包含的模板。此外,对于主模板,如果我删除 {{ define " Gold Banner"}} 语句,它是否会被渲染,如果我包含此语句,则不会呈现任何内容。
没有报错。
您在解析 goldShopRepString
后忘记重新分配 GoldShopHeaderTemplate
这一行:
GoldShopHeaderTemplate.Parse(goldShopRepString)
应该是:
GoldShopHeaderTemplate, err := GoldShopHeaderTemplate.Parse(goldShopRepString)
perl 中的应用程序通过 SSI 调用 golang 中的 header 来呈现横幅。 在 golang 中,header 模板 {gold_shop_header.shtml} 包含另一个模板 {reputation_level_info.html}。问题是包含的模板的内容没有被渲染。
参考代码:
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 }}
> {{ .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)
请指出错误之处。我是戈朗的新手。未呈现包含的模板。此外,对于主模板,如果我删除 {{ define " Gold Banner"}} 语句,它是否会被渲染,如果我包含此语句,则不会呈现任何内容。 没有报错。
您在解析 goldShopRepString
GoldShopHeaderTemplate
这一行:
GoldShopHeaderTemplate.Parse(goldShopRepString)
应该是:
GoldShopHeaderTemplate, err := GoldShopHeaderTemplate.Parse(goldShopRepString)