douluan5523 2016-11-30 02:58
浏览 33
已采纳

嵌套模板未呈现:Golang

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条回答 默认 最新

  • duanji2772 2016-11-30 04:39
    关注

    You forgot to reassign the GoldShopHeaderTemplate after you parsed goldShopRepString

    This line:

    GoldShopHeaderTemplate.Parse(goldShopRepString)
    

    Should Be:

    GoldShopHeaderTemplate, err := GoldShopHeaderTemplate.Parse(goldShopRepString)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)