But my CSS files are not working, although I have ignored that part hoping to get help when I’m done with the Logic.
I have not seen and documentations on what to do to make my css files effective on my html page, however I’m guessing the project folder structure matters, and it will probably give clues to helping me solve the problem. The structure is below.
The problem is that I have a complete project and throughout the project, if I have to serve templates, I use this;
t, err := template.ParseFiles(“templates/index.html”) // templates reside in “templates”
if err != nil{
log.Println(“error”, err)
}
t.Execute(rw, nil)
I have used the tags properly in my html and css files and so when I execute a template, I expect to see a styled page with reagrds to my css. But I see just the html skeleton. Everything else works except that. (The pages are not rendered).
How do I fix this?
How do I use Absolute Path that Matt mentioned. Kindly recommend a doc or a code sample. I use Gorilla mux
I am assuming you have a webserver and are returning HTML through handlerFuncs instead of static files.
Inside your templates you then have something like
Looking at what error you get in your browser may hint you to where your problem is.
My first guess would be that you are not actually serving the .css files. You probably want to implement a Handler for the css files, something like this:
Ok, I’m still a little confused, however I’m getting clarity on this. Like you mentioned, I am not serving the css files, and how to do that, I don’t know. Can you explain the r.Handle a little more and the parameters.
Here’s a simple example that you can try to use to help yourself with.
I’m using a similar directory structure to what you’re using here to make it easier for you to understand.
In my case the problem was that I wasn’t running the main.go from the right directory path. Read the post maybe it will help
You can check if Go an find your css files with this function below. I have left in my own comments to clarify.
// With this fucntion I can check if my filepath is working for serving static files such as CSS or Templates etc
// IMPORTANT:I failed to add static files because Go will use the current Directory you are in as the App's ROOT.
// If I run main.go from GolangBlog, the root is/Users/jorn/Documents/Golang/src/github.com/jschalkwijk/GolangBlog
// If I run it from jschalkwijk (my github folder)
_, err := os.Stat(filepath.Join(".", "YourPathToCSSFolder/css", "style.css"))
checkErr(err)
This is working for me, also with Gorrila:
As I understand it, you are telling to strip /css/ if it is there, and then load the file from a specified folder.
r.PathPrefix("/css/").Handler(http.StripPrefix("/css/",http.FileServer(http.Dir("./YourWebAppFolder/static/css/"))))