douzhao6584 2017-03-20 20:51
浏览 31
已采纳

显示模板多个变量,在其他范围内访问一个

I have a controller passing two variables

func (a App) Page() revel.Result {
    var g []*G
    ...
    return c.Render(p, g)
}

in my .html I want to iterate over g. Is it possible to access p inside the iteration? I couldnt manage. My try looks like the following

{{range .g}}
... // print g related stuff
.p
{{end}}

and it throws can't evaluate field p in type *G.

  • 写回答

1条回答 默认 最新

  • dousi1097 2017-03-20 21:22
    关注

    Revel seems to be using Go's template engine, therefore I guess you should be able to use whatever stuff's allowed with html/template like variables.

    {{$p := .p}}
    {{range .g}}
    ... // print g related stuff
    {{$p}}
    {{end}}
    

    See documentation for more info https://golang.org/pkg/text/template/#hdr-Variables

    Please note, if p is not a key in a map but a field of a struct, you won't be able to access it, i believe, since it is not exported. Ok I understand now, Revel seems to derive the names inside the template from the names of the arguments that were passed to Render.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度