I am trying to fill two menus separately. But as I can only pass one json to the template, I have to join them in some way. And then extract each menu in a go template.
What is the syntax for doing this? Is it even possible?
package main
import (
"encoding/json"
"html/template"
"os"
)
func main() {
t := template.Must(template.New("").Parse(templ))
var m []map[string]interface{}
if err := json.Unmarshal([]byte(jsondata), &m); err != nil {
panic(err)
}
if err := t.Execute(os.Stdout, m); err != nil {
panic(err)
}
}
const templ = `
<html><body>
<ul>
{{ range .main }}
<li id={{main.menu_id}}> {{main.menu_txt}}</li>
{{ end }}
</ul>
<ul>
{{ range .sub }}
<li id={{sub.menu_id}}> {{sub.menu_txt}}</li>
{{ end }}
</ul>
</body></html>`
const jsondata = `{
"main": [{
"menu_id": "1",
"menu_txt": "Home"
}, {
"menu_id": "2",
"menu_txt": "Prefs"
}],
"sub": [{
"menu_id": "3",
"menu_txt": "Test"
}, {
"menu_id": "4",
"menu_txt": "Test2"
}]
}`