I use the golang html / template package to serve content on multiple pages using the same _base.html as the frame. I combine several html files (_base.html and content file) as one.
func main() { http.HandleFunc("/", indexHandler) http.HandleFunc("/blog/", blogHandler) http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("http/css")))) http.ListenAndServe(":1337", nil) } func indexHandler(w http.ResponseWriter, r *http.Request) { index := template.Must(template.ParseFiles( "http/html/_base.html", "http/html/index.html", )) index.Execute(w, nil) } func blogHandler(w http.ResponseWriter, r *http.Request) { blog := template.Must(template.ParseFiles( "http/html/_base.html", "http/html/blog.html", )) blog.Execute(w, nil) }
By doing this at the root of my web server, my css displays fine, because the html link tag in my .css in _base.html points to the correct directory using:
<link href="css/style.css" rel="stylesheet">
however, when I move from / to / blog / my css went down one level (or I went up one level, but you would like to see it), and so css href suddenly points to /blog/css/style.css and, therefore, will not be displayed.
This can be easily fixed by indicating the css level in each content of the file that I combined with _base.html, but I feel that there should be a different, cleaner, different way. Is my mistake wrong in this case?
source share