访问HTML/Templates中结构体切片的数据

huangapple go评论108阅读模式
英文:

Accessing data of a slice of structs in html/templates

问题

我正在为学习目的构建一个论坛,并且我需要在索引中显示可用的论坛。我有一个类型为Forum的论坛:

  1. type Forum struct {
  2. Id int
  3. Name string
  4. Descr string
  5. Visibility int
  6. }

我有一个Get_Forums函数,从数据库中返回一个Forum类型的结构体切片:

  1. func Get_Forums() []Forum {
  2. db := Dbconnect()
  3. var forums []Forum
  4. rows, err := db.Query("SELECT * FROM forums")
  5. if err != nil {
  6. fmt.Println(err)
  7. }
  8. defer rows.Close()
  9. for rows.Next() {
  10. var id int
  11. var name string
  12. var descr string
  13. var visibility int
  14. err = rows.Scan(&id, &name, &descr, &visibility)
  15. forums = append(forums, Forum{Id: id, Name: name, Descr: descr, Visibility: visibility})
  16. if err != nil {
  17. fmt.Println(err)
  18. }
  19. }
  20. err = rows.Err()
  21. if err != nil {
  22. fmt.Println(err)
  23. }
  24. return forums
  25. }

现在是我的问题,我理解你通常在这里使用range,但是使用range时,所有数据将在一个声明中显示(对吗?),例如:

  1. {{range .}}
  2. {{.Id}}{{.Name}}{{.Descr}}{{.Visibility}}
  3. {{end}}

将返回:

  1. Id_1 Name_1 Desc_1 Visibility_1
  2. Id_2 Name_2 Desc_2 Visibility_2

但是我需要在页面的不同部分显示它,因为我需要将数据插入到HTML中。例如:

  1. <table border="2" cellspacing="15" cellpadding="15" align="center">
  2. <thead>
  3. <tr>
  4. <th>Forum</th>
  5. <th>Stats</th>
  6. <th>Last Messages</th>
  7. </tr>
  8. </thead>
  9. <tbody>
  10. <tr>
  11. <td><a href="/sID={{.Id}}">{{.Name}}<br></a>{{.Descr}}</td> <!-- 这里我需要第一个结构体的值 -->
  12. <td>BBBBBB</td>
  13. <td>BBBBBB</td>
  14. </tr>
  15. </tbody>
  16. </table>
  17. <br><br><br>
  18. <table border="2" cellspacing="15" cellpadding="15" align="center">
  19. <thead>
  20. <tr>
  21. <th>Forum</th>
  22. <th>Statistiche</th>
  23. <th>Ultimo Messaggio</th>
  24. </tr>
  25. </thead>
  26. <tbody>
  27. <tr>
  28. <td><a href="/sID={{.Id}}">{{.Name}}<br></a>{{.Descr}}</td> <!-- 这里我需要第二个结构体的值 -->
  29. <td>AAAAA</td>
  30. <td>AAAAAA</td>
  31. </tr>
  32. </tbody>
  33. </table>
  34. </div>

编辑:对于@jasonohlmsted

  1. <body>
  2. {{with index . 0}}
  3. <!-- 这里显示第一个结构体的值 -->
  4. <a href="/sID={{.Id}}">{{.Name}}<br></a>{{.Descr}}</td>
  5. {{end}}
  6. {{with index . 1}}
  7. <!-- 这里显示第二个结构体的值 -->
  8. <a href="/sID={{.Id}}">{{.Name}}<br></a>{{.Descr}}
  9. {{end}}
  10. </body>
  1. func index(w http.ResponseWriter, r *http.Request) {
  2. var forums []Forum
  3. forums = append(forums, Forum{1, "sez1", "desc1"})
  4. forums = append(forums, Forum{2, "sez2", "desc2"})
  5. tpl.ExecuteTemplate(w, "index.html", forums)
  6. }
  7. type Forum struct {
  8. Id int
  9. Name string
  10. Descr string
  11. }

第一个索引工作正常,但第二个索引不显示。

英文:

I'm building a forum for learning purposes and I need to show in the index the forums available. I have a type forum:

  1. type Forum struct {
  2. Id int
  3. Name string
  4. Descr string
  5. Visibility int
  6. }

I have a Get_Forums function that return a slice of structs type forum from the db:

  1. func Get_Forums() []Forum {
  2. db := Dbconnect()
  3. var forums []Forum
  4. rows, err := db.Query(&quot;SELECT * FROM forums&quot;)
  5. if err != nil {
  6. fmt.Println(err)
  7. }
  8. defer rows.Close()
  9. for rows.Next() {
  10. var id int
  11. var name string
  12. var descr string
  13. var visibility int
  14. err = rows.Scan(&amp;id, &amp;name, &amp;descr, &amp;visibility)
  15. forums = append(forums, Forum{Id: id, Name: name, Descr: descr, Visibility: visibility})
  16. if err != nil {
  17. fmt.Println(err)
  18. }
  19. }
  20. err = rows.Err()
  21. if err != nil {
  22. fmt.Println(err)
  23. }
  24. return forums
  25. }

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:

  1. {{range .}}
  2. {{.Id}}{{.Name}}{{.Descr}}{{.Visibility}}
  3. {{end}}

Will return:

  1. Id_1 Name_1 Desc_1 Visibility_1
  2. 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:

  1. &lt;table border=&quot;2&quot; cellspacing=&quot;15&quot; cellpadding=&quot;15&quot; align=&quot;center&quot;&gt;
  2. &lt;thead&gt;
  3. &lt;tr&gt;
  4. &lt;th&gt;Forum&lt;/th&gt;
  5. &lt;th&gt;Stats&lt;/th&gt;
  6. &lt;th&gt;Last Messages&lt;/th&gt;
  7. &lt;/tr&gt;
  8. &lt;/thead&gt;
  9. &lt;tbody&gt;
  10. &lt;tr&gt;
  11. &lt;td&gt;&lt;a href=&quot;/sID={{.Id}}&quot;&gt;{{.Name}}&lt;br&gt;&lt;/a&gt;{{.Descr}}&lt;/td&gt; &lt;!-- Here I need the first struct values --&gt;
  12. &lt;td&gt;BBBBBB&lt;/td&gt;
  13. &lt;td&gt;BBBBBB&lt;/td&gt;
  14. &lt;/tr&gt;
  15. &lt;/tbody&gt;
  16. &lt;/table&gt;
  17. &lt;br&gt;&lt;br&gt;&lt;br&gt;
  18. &lt;table border=&quot;2&quot; cellspacing=&quot;15&quot; cellpadding=&quot;15&quot; align=&quot;center&quot;&gt;
  19. &lt;thead&gt;
  20. &lt;tr&gt;
  21. &lt;th&gt;Forum&lt;/th&gt;
  22. &lt;th&gt;Statistiche&lt;/th&gt;
  23. &lt;th&gt;Ultimo Messaggio&lt;/th&gt;
  24. &lt;/tr&gt;
  25. &lt;/thead&gt;
  26. &lt;tbody&gt;
  27. &lt;tr&gt;
  28. &lt;td&gt;&lt;a href=&quot;/sID={{.Id}}&quot;&gt;{{.Name}}&lt;br&gt;&lt;/a&gt;{{.Descr}}&lt;/td&gt; &lt;!-- Here I need the second struct values --&gt;
  29. &lt;td&gt;AAAAA&lt;/td&gt;
  30. &lt;td&gt;AAAAAA&lt;/td&gt;
  31. &lt;/tr&gt;
  32. &lt;/tbody&gt;
  33. &lt;/table&gt;
  34. &lt;/div&gt;

EDIT: For @jasonohlmsted

  1. &lt;body&gt;
  2. {{with index . 0}}
  3. &lt;!-- Here I show the first struct values --&gt;
  4. &lt;a href=&quot;/sID={{.Id}}&quot;&gt;{{.Name}}&lt;br&gt;&lt;/a&gt;{{.Descr}}&lt;/td&gt;
  5. {{end}}
  6. {{with index . 1}}
  7. &lt;!-- Here I show the second struct values --&gt;
  8. &lt;a href=&quot;/sID={{.Id}}&quot;&gt;{{.Name}}&lt;br&gt;&lt;/a&gt;{{.Descr}}
  9. {{end}}
  10. &lt;/body&gt;
  11. func index(w http.ResponseWriter, r *http.Request) {
  12. var forums []Forum
  13. forums = append(forums, Forum{1, &quot;sez1&quot;, &quot;desc1&quot;})
  14. forums = append(forums, Forum{2, &quot;sez2&quot;, &quot;desc2&quot;})
  15. tpl.ExecuteTemplate(w, &quot;index.html&quot;, forums)
  16. }
  17. type Forum struct {
  18. Id int
  19. Name string
  20. Descr string
  21. }

The first index works fine but the second one don't show

答案1

得分: 2

使用内置的索引函数来索引切片:

  1. ...
  2. {{with index . 0}}
  3. <!-- 这里展示第一个结构体的值 -->
  4. <td><a href="/sID={{.Id}}">{{.Name}}<br></a>{{.Descr}}</td>
  5. {{end}}
  6. ...
  7. {{with index . 1}}
  8. <!-- 这里展示第二个结构体的值 -->
  9. <td><a href="/sID={{.Id}}">{{.Name}}<br></a>{{.Descr}}</td>
  10. {{end}}
  11. ...

可运行示例

英文:

Use builtin index function to index into a slice:

  1. ...
  2. {{with index . 0}}
  3. &lt;!-- Here I show the first struct values --&gt;
  4. &lt;td&gt;&lt;a href=&quot;/sID={{.Id}}&quot;&gt;{{.Name}}&lt;br&gt;&lt;/a&gt;{{.Descr}}&lt;/td&gt;
  5. {{end}}
  6. ...
  7. {{with index . 1}}
  8. &lt;!-- Here I show the second struct values --&gt;
  9. &lt;td&gt;&lt;a href=&quot;/sID={{.Id}}&quot;&gt;{{.Name}}&lt;br&gt;&lt;/a&gt;{{.Descr}}&lt;/td&gt;
  10. {{end}}
  11. ...

Runnable example.

huangapple
  • 本文由 发表于 2023年1月15日 07:13:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75121905.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定