Let's say I have a page, it a header and a body. In the header are links, and on click the body changes, but the header remains. To build this with the html/template
library is easy, but also seems dumb if I just send back a whole new page (fetching the information in the header from the database every time). How to I switch out body templates depending on the url
I guess.
Here is what I have:
`
{{template "GlobalNav"}}
{{template "GroupHeader" .Header }}
{{ if eq .Active "" }}
{{ template "GroupBody" .Body }}
{{ else if eq .Active "papers" }}
{{ template "GroupPapers" .Body }}
{{ else if eq .Active "projects" }}
{{ template "GroupProjects" .Body }}
{{ end }}`
Server Side:
`http.HandleFunc("/g/", Groups)
http.HandleFunc("/g/papers", GroupsPapers)
http.HandleFunc("/g/projects", GroupsProjects)
func Groups() {
header := fromDBHeader(id)
body := fromDBMain(id)
render Home template ...
}
func GroupsPapers() {
header := fromDBHeader(id)
body := fromDBPapers(id)
render Paper template ...
}
func GroupsProjects() {
header := fromDBHeader(id)
body := fromDBProjects(id)
render Project template ...
}
`
Is it time for some JS?
try this way put the html files to html folder add html and js files to it.
func webServer() {
http.Handle(
"/",
http.StripPrefix(
"/",
http.FileServer(http.Dir("html")),
),
)
http.ListenAndServe(":9000", nil)
}
AND BROWSE IT UNDER http://localhost:9000/