dongzan9069 2015-05-18 13:19
浏览 3
已采纳

转到:template.ParseFiles()不适用于{{.active}},但适用于{{printf“%s” .active}}

Lets say the body of my html file like so

<body>
    <h2>Current number of players: {{.active}}</h2>
</body>

And my go code looks like

type page struct{
    active string
}
t, _ template.ParseFiles("page.html")
t.Execute(w,page{active: "No Players are Online"})

When I run the code, I get a blank screen. When I change {{.active}} to {{printf "%s" .active}} it works.

Do I always need to include printf? I guess I'm confused by the documentation.

Thanks!

  • 写回答

1条回答 默认 最新

  • dongpai6567 2015-05-18 13:22
    关注

    Make active property capitalized. Like so:

    type page struct{
        Active string
    }
    t, _ template.ParseFiles("page.html")
    t.Execute(w,page{Active: "No Players are Online"})
    

    and template

    <body>
        <h2>Current number of players: {{.Active}}</h2>
    </body>
    

    Go exports only capitalized struct properties to other modules.

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料