英文:
Accessing data of a slice of structs in html/templates
问题
我正在为学习目的构建一个论坛,并且我需要在索引中显示可用的论坛。我有一个类型为Forum的论坛:
type Forum struct {
Id int
Name string
Descr string
Visibility int
}
我有一个Get_Forums函数,从数据库中返回一个Forum类型的结构体切片:
func Get_Forums() []Forum {
db := Dbconnect()
var forums []Forum
rows, err := db.Query("SELECT * FROM forums")
if err != nil {
fmt.Println(err)
}
defer rows.Close()
for rows.Next() {
var id int
var name string
var descr string
var visibility int
err = rows.Scan(&id, &name, &descr, &visibility)
forums = append(forums, Forum{Id: id, Name: name, Descr: descr, Visibility: visibility})
if err != nil {
fmt.Println(err)
}
}
err = rows.Err()
if err != nil {
fmt.Println(err)
}
return forums
}
现在是我的问题,我理解你通常在这里使用range,但是使用range时,所有数据将在一个声明中显示(对吗?),例如:
{{range .}}
{{.Id}}{{.Name}}{{.Descr}}{{.Visibility}}
{{end}}
将返回:
Id_1 Name_1 Desc_1 Visibility_1
Id_2 Name_2 Desc_2 Visibility_2
但是我需要在页面的不同部分显示它,因为我需要将数据插入到HTML中。例如:
<table border="2" cellspacing="15" cellpadding="15" align="center">
<thead>
<tr>
<th>Forum</th>
<th>Stats</th>
<th>Last Messages</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="/sID={{.Id}}">{{.Name}}<br></a>{{.Descr}}</td> <!-- 这里我需要第一个结构体的值 -->
<td>BBBBBB</td>
<td>BBBBBB</td>
</tr>
</tbody>
</table>
<br><br><br>
<table border="2" cellspacing="15" cellpadding="15" align="center">
<thead>
<tr>
<th>Forum</th>
<th>Statistiche</th>
<th>Ultimo Messaggio</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="/sID={{.Id}}">{{.Name}}<br></a>{{.Descr}}</td> <!-- 这里我需要第二个结构体的值 -->
<td>AAAAA</td>
<td>AAAAAA</td>
</tr>
</tbody>
</table>
</div>
编辑:对于@jasonohlmsted
<body>
{{with index . 0}}
<!-- 这里显示第一个结构体的值 -->
<a href="/sID={{.Id}}">{{.Name}}<br></a>{{.Descr}}</td>
{{end}}
{{with index . 1}}
<!-- 这里显示第二个结构体的值 -->
<a href="/sID={{.Id}}">{{.Name}}<br></a>{{.Descr}}
{{end}}
</body>
func index(w http.ResponseWriter, r *http.Request) {
var forums []Forum
forums = append(forums, Forum{1, "sez1", "desc1"})
forums = append(forums, Forum{2, "sez2", "desc2"})
tpl.ExecuteTemplate(w, "index.html", forums)
}
type Forum struct {
Id int
Name string
Descr string
}
第一个索引工作正常,但第二个索引不显示。
英文:
I'm building a forum for learning purposes and I need to show in the index the forums available. I have a type forum:
type Forum struct {
Id int
Name string
Descr string
Visibility int
}
I have a Get_Forums function that return a slice of structs type forum from the db:
func Get_Forums() []Forum {
db := Dbconnect()
var forums []Forum
rows, err := db.Query("SELECT * FROM forums")
if err != nil {
fmt.Println(err)
}
defer rows.Close()
for rows.Next() {
var id int
var name string
var descr string
var visibility int
err = rows.Scan(&id, &name, &descr, &visibility)
forums = append(forums, Forum{Id: id, Name: name, Descr: descr, Visibility: visibility})
if err != nil {
fmt.Println(err)
}
}
err = rows.Err()
if err != nil {
fmt.Println(err)
}
return forums
}
And now come my problem, as i understood you typically use a range here but with a range all data will be shown with one declaration (right?), example:
{{range .}}
{{.Id}}{{.Name}}{{.Descr}}{{.Visibility}}
{{end}}
Will return:
Id_1 Name_1 Desc_1 Visibility_1
Id_2 Name_2 Desc_2 Visibility_2
But I need to display it in different part of the page because i need to insert the data inside html. Example:
<table border="2" cellspacing="15" cellpadding="15" align="center">
<thead>
<tr>
<th>Forum</th>
<th>Stats</th>
<th>Last Messages</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="/sID={{.Id}}">{{.Name}}<br></a>{{.Descr}}</td> <!-- Here I need the first struct values -->
<td>BBBBBB</td>
<td>BBBBBB</td>
</tr>
</tbody>
</table>
<br><br><br>
<table border="2" cellspacing="15" cellpadding="15" align="center">
<thead>
<tr>
<th>Forum</th>
<th>Statistiche</th>
<th>Ultimo Messaggio</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="/sID={{.Id}}">{{.Name}}<br></a>{{.Descr}}</td> <!-- Here I need the second struct values -->
<td>AAAAA</td>
<td>AAAAAA</td>
</tr>
</tbody>
</table>
</div>
EDIT: For @jasonohlmsted
<body>
{{with index . 0}}
<!-- Here I show the first struct values -->
<a href="/sID={{.Id}}">{{.Name}}<br></a>{{.Descr}}</td>
{{end}}
{{with index . 1}}
<!-- Here I show the second struct values -->
<a href="/sID={{.Id}}">{{.Name}}<br></a>{{.Descr}}
{{end}}
</body>
func index(w http.ResponseWriter, r *http.Request) {
var forums []Forum
forums = append(forums, Forum{1, "sez1", "desc1"})
forums = append(forums, Forum{2, "sez2", "desc2"})
tpl.ExecuteTemplate(w, "index.html", forums)
}
type Forum struct {
Id int
Name string
Descr string
}
The first index works fine but the second one don't show
答案1
得分: 2
使用内置的索引函数来索引切片:
...
{{with index . 0}}
<!-- 这里展示第一个结构体的值 -->
<td><a href="/sID={{.Id}}">{{.Name}}<br></a>{{.Descr}}</td>
{{end}}
...
{{with index . 1}}
<!-- 这里展示第二个结构体的值 -->
<td><a href="/sID={{.Id}}">{{.Name}}<br></a>{{.Descr}}</td>
{{end}}
...
英文:
Use builtin index function to index into a slice:
...
{{with index . 0}}
<!-- Here I show the first struct values -->
<td><a href="/sID={{.Id}}">{{.Name}}<br></a>{{.Descr}}</td>
{{end}}
...
{{with index . 1}}
<!-- Here I show the second struct values -->
<td><a href="/sID={{.Id}}">{{.Name}}<br></a>{{.Descr}}</td>
{{end}}
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论