doutou7286 2016-02-17 00:41
浏览 21
已采纳

具有变量的Golang / Revel模板引擎模板

Is there a way to pass a variable that was iterated over into a Golang/Revel template?

For example, in "header.html", I have

{{range .templates}}
    {{template "something" .}}
{{end}}

How can I use current index from the array as an argument to template? I tried embedding another {{.}} as shown in the Revel examples, but that leads to a template compliation error. Would the variable be something like $i?

For example, iterating through in Revel is done like so

{{range .messages}}
    <p>{{.}}</p>
{{end}}

However, I read that the . means nil.... how does this work in Revel?

  • 写回答

1条回答 默认 最新

  • dou4624 2016-02-17 03:07
    关注

    If I understand your question correctly, you can use the range built-in to get the index, and then pass it to the template like this:

    {{range $i, $t := .templates}}
       {{template "Template.html" $i}}
    {{end}}
    

    So if the templates variable was defined like this:

    templates := []string{"One", "Two"}
    

    and Template.html contains:

    This is from Template.html: {{ . }}<br>
    

    Then the final output will be:

    This is from Template.html: 0<br>
    This is from Template.html: 1<br>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line