doulue7522 2015-10-23 19:44
浏览 141

当我在golang html模板中显示变量时调用什么方法

I am trying to figure out what method is called when I show a variable in an "html/template" template in Go via {{ .SomeVariable }}.

I am using the package "html/template".

I am using this function to render a template:

func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
    t, _ := template.ParseFiles("public/" + tmpl + ".html")
    t.Execute(w, p)
}

with

type Page struct {
    Title  string
    Users []*model.User
}

and an example template

<html>
  <head>
    <title>Listing Users</title>
  </head>
  <body>
    <h1>Listing users</h1>

    {{range .Params}}
      <h3>
        id {{.Id }}
        &nbsp;
        email {{.Email}}
        &nbsp;
         name {{.Name}}
      </h3>
    {{end}}

  </body>
</html>

We store Id in a certain way and have a .String() method defined for displaying it.

When we use the package "text/template" the Id attribute displays correctly but when we use "html/template" it does not. My guess is that the former is calling .String() when displaying a variable and latter is not. I haven't been able to glean from the documentation what method is being called.

This is the first SO question I've ever written for Go. Hopefully made it clear but feel free to ask for additional info as I am a complete Go noob.

Thanks.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突
    • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
    • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序