嵌套模板未被渲染:Golang

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

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" }}
        <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>


{{ 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 &quot;Gold Banner&quot;}}
    .
    .
     &lt;div class=&quot;text-center mt-3&quot;&gt;
        &lt;span class=&quot;fs-12 font-default cursor-default&quot;&gt;
              {{ Loc .Lang &quot;Reputation&quot; }}
              {{ template &quot;reputation_level&quot; . }}
        &lt;/span&gt;
     &lt;/div&gt;
     .
     .
   {{ end }}


reputation_level_info.html

    {{define &quot;reputation_level&quot;}}
    &lt;i class=&quot;icon-help-alt ml-5&quot;&gt;
     &lt;div class=&quot;absolute text-left&quot; style=&quot;&quot;&gt;
        &lt;div class=&quot;relative hover-reputation-lib&quot;&gt;
            &lt;div class=&quot;relative w-100p&quot;&gt;
                &lt;div class=&quot;fs-18 mt-5&quot;&gt;{{ Loc .Lang &quot;Reputation Levels&quot; }}&lt;/div&gt;

                &lt;table class=&quot; mt-10 fs-8 table-repsys&quot; &gt;
                    &lt;tbody&gt;
                        &lt;tr class=&quot;bold&quot;&gt;
                            &lt;th&gt;{{ Loc .Lang &quot;Badges&quot; }}&lt;/th&gt;
                            &lt;th&gt;{{ Loc .Lang &quot;Name&quot; }}&lt;/th&gt;
                            &lt;th&gt;{{ Loc .Lang &quot;Points&quot; }}&lt;/th&gt;
                        &lt;/tr&gt;

                        {{ range .RepLevels }}
                        &lt;tr class=&quot;{{ .Class}}&quot;&gt;
                            &lt;td&gt;
                                &lt;span class=&quot;badges-lib {{ .ImageName}}&quot;&gt;&lt;/span&gt;
                            &lt;/td&gt;

                            {{ if eq .Badges &quot;off&quot; }}
                            &lt;td&gt;
                                {{ .Badges }}
                            &lt;/td&gt;
                            &lt;td&gt;
                                {{ if .UpperBound }}
                                    {{ .LowerBound }} - {{ .UpperBound }} Points
                                {{ else }}
                                    &amp;gt; {{ .LowerBound }} Points
                                {{ end }}
                            &lt;/td&gt;
                            {{ else }}
                                &lt;td colspan=&quot;2&quot;&gt;{{ Loc ..Lang &quot;Reputation Off&quot; }}&lt;/td&gt;
                            {{ end }}
                        &lt;/tr&gt;
                        {{ end }}
                    &lt;/tbody&gt;
                &lt;/table&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/i&gt;
{{ end }}




gen.go
  
   var reputationTemplatePath = &quot;/var/ssi/banner/gold/reputation_level_info.html&quot;

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

    var goldShopRepString = string(_goldShopReputationTemplateBytes)

    var headerTemplatePath = &quot;/var/ssi/banner/gold/gold_shop_header.shtml&quot;

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

    var goldShopHeaderString = string(_goldShopHeaderTemplateBytes)

    var templatesString [] string

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


htmlBuffer := bytes.NewBufferString(&quot;&quot;)
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)

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:

确定