Hello, I am building a site which will have the following pages, "/" (home), "/about", and "/data" which will show some data using DataTables <https://datatables.net/examples/index>.
"/", "/about" will be simple so I am using templates/base.html but for
/data I would still like to use tempaltes/data.html because I need to load
DataTables code. To do this I am creating a seperate handler and I am not
sure if this is the right way to do it.
So, is a seperate handler needed? and how inefficient is it?
var templates = template.Must(template.ParseFiles("templates/base.html",
"templates/data.html"))
func handler(w http.ResponseWriter, r *http.Request) {
log.Printf(r.URL.Path[1:])
myVars :=
Variables{"Main", "My Heading #1"}
err :=
templates.ExecuteTemplate(w, "base.html", myVars)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func staticHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, r.URL.Path[1:])
}
func dataHandler(w http.ResponseWriter, r *http.Request) {
myVars := Variables{"About", "About page"}
err := templates.ExecuteTemplate(w, "data.html", myVars)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func main() {
fmt.Printf("ok\n")
var port string = ":9000"
log.Printf("starting up on %s", port)
http.HandleFunc("/", handler)
http.HandleFunc("/data", dataHandler)
http.HandleFunc("/static/", staticHandler)
http.ListenAndServe(port, nil)
}
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.
