Home > Back-end >  Accessing data of a slice of structs in html/templates
Accessing data of a slice of structs in html/templates

Time:01-15

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

CodePudding user response:

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}}
...

Runnable example.

  •  Tags:  
  • go
  • Related