dougaopu7938 2014-02-18 19:42
浏览 98
已采纳

在html模板golang中的许多结构的切片中访问结构变量

I'm attempting to send a slice containing many structs to an html template.

I have a 'post' struct

type Post struct {
  threadID int
  subject string
  name string
  text string
  date_posted string
}

I create a slice of type Post ( posts := []Post{} )

this slice is then populated using rows from my database and then executed on my template.

defer latest_threads.Close()
for latest_threads.Next(){
    var threadID int
    var subject string
    var name string
    var text string
    var date_posted string
    latest_threads.Scan(&threadID, &subject, &name, &text, &date_posted) 
    post := Post{
        threadID,
        subject,
        name,
        text,
        date_posted,
    }
    posts = append(posts, post)
}
t, error := template.ParseFiles("thread.html")
if error != nil{
    log.Fatal(error)
}
t.Execute(w, posts)
}

The program compiles / runs okay but when viewing the html output from the template

{{.}}
{{range .}}
    <div>{{.threadID}}</div>
    <h3>{{.subject}}</h3>
    <h3>{{.name}}</h3>
    <div>{{.date_posted}}</div>
    <div><p>{{.text}}</p></div>
    <br /><br />
{{end}}

{{.}} outputs just fine however upon reaching the first {{.threadID}} in {{range .}} the html stops.

<!DOCTYPE html>
<html>
<head>
    <title> Test </title>
</head>
<body>
    //here is where {{.}} appears just fine, removed for formatting/space saving
    <div>
  • 写回答

1条回答 默认 最新

  • doulong1987 2014-02-18 20:31
    关注

    It's not really intuitive, but templates (and encoding packages like JSON, for that matter) can't access unexported data members, so you have to export them somehow:

    Option 1

    // directly export fields
    type Post struct {
        ThreadID int
        Subject, Name, Text, DatePosted string
    }
    

    Option 2

    // expose fields via accessors:
    type Post struct {
        threadID int
        subject, name, text, date_posted string
    }
    
    func (p *Post) ThreadID()   int    { return p.threadID    }
    func (p *Post) Subject()    string { return p.subject     }
    func (p *Post) Name()       string { return p.name        }
    func (p *Post) Text()       string { return p.text        }
    func (p *Post) DatePosted() string { return p.date_posted }
    

    Update template

    (this part is mandatory regardless of which option you chose from above)

    {{.}}
    {{range .}}
        <div>{{.ThreadID}}</div>
        <h3>{{.Subject}}</h3>
        <h3>{{.Name}}</h3>
        <div>{{.DatePosted}}</div>
        <div><p>{{.Text}}</p></div>
        <br /><br />
    {{end}}
    

    And this should work.

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

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭