Reputation: 885
coders.
I need to render the nested struct data in go template. I wonder if it possible to do with nested loops in .gohtml
template file.
Here is my .gohtml
code:
<!DOCTYPE html>
<html>
<head>
<meta charser="utf-8" />
<title>Go templates</title>
</head>
<body>
<ul>
{{range $city := .}}
<li>
name: {{$city.name}}
hotels:
<ul>
{{range $hotel := $city.hotels}}
<li>
name: {{$hotel.name}}
address: {{$hotel.address}}
zip: {{$hotel.zip}}
</li>
{{end}}
</ul>
</li>
{{end}}
</ul>
</body>
</html>
Here is main.go
code:
package main
import (
"os"
"text/template"
)
var tpl *template.Template
func init() {
tpl = template.Must(template.New("").ParseGlob("./*.gohtml"))
}
func main() {
type hotel struct {
name string
address string
city string
zip int
}
type city struct {
name string
hotels []hotel
}
type region struct {
cities []city
}
hotel1 := hotel{
"Lambda",
"Street 19",
"Some city",
65530,
}
hotel2 := hotel{
"Black Sea",
"Street 21",
"Some city",
65543,
}
hotel3 := hotel{
"Blue Sea",
"Street 15",
"Some city",
54400,
}
hotel4 := hotel{
"Yellow Submarine",
"The Beatles Square",
"Some city",
54401,
}
hotel5 := hotel{
"LolKek",
"Cheburek",
"Some city",
14213,
}
city1 := city{
"Some city",
[]hotel{hotel1, hotel2},
}
city2 := city{
"Some city",
[]hotel{hotel3, hotel4},
}
city3 := city{
"Some city",
[]hotel{hotel5},
}
someRegion := region{
[]city{city1, city2, city3},
}
tpl.ExecuteTemplate(os.Stdout, "tpl.gohtml", someRegion)
}
There is no error in the terminal when go run main.go
, but I don't understand why the output looks like this:
<!DOCTYPE html>
<html>
<head>
<meta charser="utf-8" />
<title>Go templates</title>
</head>
<body>
<ul>
Why is it cutted?
Upvotes: 3
Views: 1667
Reputation: 417612
You don't see any errors because you're not checking errors, you're omitting them.
Template.ExecuteTemplate()
returns an error, do check it:
if err := tpl.ExecuteTemplate(os.Stdout, "tpl.gohtml", someRegion); err != nil {
fmt.Println(err)
}
This will output:
template: :9:29: executing "" at <.>: range can't iterate over {[{Some city [{Lambda Street 19 Some city 65530} {Black Sea Street 21 Some city 65543}]} {Some city [{Blue Sea Street 15 Some city 54400} {Yellow Submarine The Beatles Square Some city 54401}]} {Some city [{LolKek Cheburek Some city 14213}]}]}
The error is clear: you pass a struct for execution, and then you try to range over it. You can't. Range over the slice:
{{range $city := .cities}}
This of course won't work: you have to export the struct field to be able to access it inside the template.
type region struct {
Cities []city
}
And in template:
{{range $city := .Cities}}
You also have to export other struct fields:
type hotel struct {
Name string
Address string
City string
Zip int
}
type city struct {
Name string
Hotels []hotel
}
After these changes it will work and output (try it on the Go Playground):
<!DOCTYPE html>
<html>
<head>
<meta charser="utf-8" />
<title>Go templates</title>
</head>
<body>
<ul>
<li>
name: Some city
hotels:
<ul>
<li>
name: Lambda
address: Street 19
zip: 65530
</li>
<li>
name: Black Sea
address: Street 21
zip: 65543
</li>
</ul>
</li>
<li>
name: Some city
hotels:
<ul>
<li>
name: Blue Sea
address: Street 15
zip: 54400
</li>
<li>
name: Yellow Submarine
address: The Beatles Square
zip: 54401
</li>
</ul>
</li>
<li>
name: Some city
hotels:
<ul>
<li>
name: LolKek
address: Cheburek
zip: 14213
</li>
</ul>
</li>
</ul>
</body>
</html>
Upvotes: 5