I’m making an attempt to fill two menus individually. However as I can solely move one json to the template, I’ve to affix them in a roundabout way. After which extract every menu in a go template.
What’s the syntax for doing this? Is it even attainable?
bundle foremost
import (
"encoding/json"
"html/template"
"os"
)
func foremost() {
t := template.Should(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><physique>
<ul>
{{ vary .foremost }}
<li id={{foremost.menu_id}}> {{foremost.menu_txt}}</li>
{{ finish }}
</ul>
<ul>
{{ vary .sub }}
<li id={{sub.menu_id}}> {{sub.menu_txt}}</li>
{{ finish }}
</ul>
</physique></html>`
const jsondata = `{
"foremost": [{
"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"
}]
}`
I feel I discovered one answer:
Another answer?