Database range inside range html template

How to display range inside range from database to html template?

for col.Next(){
for col1.Next(){

}
}

You’d be better to convert your rows from the database into something meaningful like a slice of structs of a specific type, then pass that in to the template. Think of the template as just about presentation, not about logic for extracting data which should happen first in your handler.

So in handler something like:

rows, err := db.Query(q, age)
if err != nil {
        log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
      ...scan into struct here
}

So if you need to do complex logic like nesting you can do it there, but it’s not something to attempt in a template.

Then in the template you can just do a normal range with your data:

{{ range .items }}
  .Name
{{ end }}
2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.