I'm having a weird issue with Go templates. For some reason when I use double range
it stops rendering everything below it in the code.
// Index.html
{{define "index"}}
{{range $k, $element := .Items}}
{{range $element}}
{{.Title}}
{{end}}
{{end}}
{{end}}
This is my Go code:
data := IndexData{
Items: items,
}
IndexTemplate := template.Must(template.New("skeleton.html").Funcs(FuncTemplate).ParseFiles("skeleton.html", "index.html"))
IndexTemplate.ExecuteTemplate(w, "skeleton", data)
It does show the data correctly on my page and there are no errors. The only problem here is that it stops the rendering of the page after the last item is displayed.
In my skeleton I display my templates like this, depending on what page they're visiting:
// Skeleton.html
{{define "skeleton"}}
{{block "index".}}{{end}}
{{block "account.register".}}{{end}}
{{block "account.login".}}{{end}}
{{block "account.profile".}}{{end}}
{{end}}
Why does it stop rendering after the last item is displayed from the range?
EDIT:
Only error displayed is executing "index" at <$element>: range can't iterate over true
EDIT 2:
.Item
is a map[string]interface{}
that contains the following:
map[result:[map[Title:Hello World2 Content:Lorem ipsum dolor sit amet2...] map[Title:Hello World Content:Lorem ipsum dolor sit amet...]] success:true]
Solution
I managed to solve this by properly returning the data I need to use without the success:true
part as well as using it as interface{}
so I don't have to use 2 range loops.