Reputation: 357
I want to specify an html template in one of my golang controller My directory structure is like this
Project
-com
-src
- controller
-contoller.go
-view
- html
-first.html
I want to load first.html for request /new .I have used NewHandler for url /new and the NewHandler func is executing when /new request comes and is in controller.go. Here is my code
func NewHandler(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("view/html/first.html")
if err == nil {
log.Println("Template parsed successfully....")
}
err := templates.ExecuteTemplate(w, "view/html/first.html", nil)
if err != nil {
log.Println("Not Found template")
}
// t.Execute(w, "")
}
But I am getting an error
panic: open first.html: no such file or directory
Please help me to remove this error. Thanks in advance
Upvotes: 8
Views: 26028
Reputation: 4209
Solution, where I just traversed outside the directory without using libraries to find the path or convert them.
Using "os" package get the present working directory. Like,
import(
"os"
)
your_function(){
pwd,_ := os.pwd()}
Above snippet will return you the path where your program is trying to find the file.
Like
D:\Golang_Projects\POC\Distributed_Application_Go\cmd\teacherportal
After you're sure what path your program is looking at, use ".." to come outside of that directory. Like,
rootTemplate,err := template.ParseFiles("../../teacherportal/students.gohtml")
Alternative solutions are also there but I found this quite simple to implement.
Upvotes: 0
Reputation: 416
Better solution is to give absolute path name i.e instead of this "view/html/first.html",try this
func NewHandler(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("./work/src/Project/view/html/first.html")
if err == nil {
log.Println("Template parsed successfully....")
}
err := templates.ExecuteTemplate(w, "view/html/first.html", nil)
if err != nil {
log.Println("Not Found template")
}
// t.Execute(w, "")
}
Upvotes: 0
Reputation: 146
Have you included this line in the main function?
http.Handle("/view/", http.StripPrefix("/view/", http.FileServer(http.Dir("view"))))
view is the name of the directory that has to be specified in FileServer function to allow read/write.(view directory has to be kept in the same directory where your binary is present)
Upvotes: 1
Reputation: 357
I have solved the issue by giving absolute path of the html. For that I created a class in which the html are parsed
package htmltemplates
import (
"html/template"
"path/filepath"
)
And in the NewHandler method I removed //Templates is used to store all Templates var Templates *template.Template
func init() {
filePrefix, _ := filepath.Abs("./work/src/Project/view/html/") // path from the working directory
Templates = template.Must(template.ParseFiles(filePrefix + "/first.html"))
...
//htmls must be specified here to parse it
}
And in the NewHandler I removed first 5 lines and instead gave
err := htmltemplates.Templates.ExecuteTemplate(w, "first.html", nil)
It is now working .But need a better solution if any
Upvotes: 9