英文:
go - open file no such file or directory
问题
我写了一个包装函数来渲染模板,代码如下:
func RenderTemplate(w http.ResponseWriter, data interface{}, tmpl... string) {
cwd, _ := os.Getwd()
for _ , file := range tmpl{
file = filepath.Join(cwd,"./view/"+file+".html")
}
t, err := template.ParseFiles(tmpl...)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = t.Execute(w, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
我在 main
函数中设置了静态文件夹和路由处理程序:
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("public"))))
http.HandleFunc("/", route.IndexHandler)
这是我的路由处理程序:
func IndexHandler(w http.ResponseWriter, r *http.Request) {
files := []string{"base", "index"}
util.RenderTemplate(w, nil, files...)
}
我打算使用模板嵌套来帮助 index.html
扩展 base.html
。
这是 base.html
的内容:
{{define "base"}}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{{template "title" .}}</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/js/isotope.pkgd.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="/css/style.css">
</head>
<body>
{{template "index" .}}
</body>
</html>
{{end}}
这是 index.html
的内容:
<!DOCTYPE html>
<html>
{{define "title"}}Homepage{{end}}
<body>
{{define "index"}}
<div class="wrapper">
<div class="page-content">
<div class="container">
<div class="left">
<img src="../public/images/img_landing_page_mac.png">
</div>
<div class="right">
<h2 style="font-size: 33px; letter-spacing: 5px">Organize <br>Modern Knowledge<br> for Mankind</h2>
<p style="font-size: 20px;margin-top: 35px;letter-spacing: 4px">Consume, Colect and Revisit <br>Knowledge at Your Fingertips</p>
<a href="#" style="margin-top: 80px;display: inline-block;margin-left: -17px"><img src="../public/images/btn_get_chrome.png"></a>
</div>
</div>
</div>
</div>
{{end}}
</body>
</html>
但是当我运行服务器并在浏览器上请求路径时,显示出错信息 open base: no such file or directory
。我的渲染函数有问题吗?
英文:
I write a wrapper function to render template like this
func RenderTemplate(w http.ResponseWriter, data interface{}, tmpl... string) {
cwd, _ := os.Getwd()
for _ , file := range tmpl{
file = filepath.Join(cwd,"./view/"+file+".html")
}
t, err := template.ParseFiles(tmpl...)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = t.Execute(w, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
I setup static folder and route handler in main
http.Handle("/public/",http.StripPrefix("/public/",http.FileServer(http.Dir("public"))))
http.HandleFunc("/",route.IndexHandler)
And this is my route handler
func IndexHandler(w http.ResponseWriter, r *http.Request) {
files:=[]string{"base","index"}
util.RenderTemplate(w, nil, files...)
}
I intend to use template nesting to help index.html
extend base.html
This is base.html
{{define "base"}}
<!DOCTYPE html>
<html>
<head>
<meta charget="utf-8">
<title>{{template "title".}}</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/js/isotope.pkgd.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="/css/style.css">
</head>
<body>
{{template "index".}}
</body>
</html>
{{end}}
This is index.html
<!DOCTYPE html>
<html>
{{define "title"}}Homepage{{end}}
<body>
{{define "index"}}
<div class="wrapper">
<div class="page-content">
<div class="container">
<div class="left">
<img src="../public/images/img_landing_page_mac.png">
</div>
<div class="right">
<h2 style="font-size: 33px; letter-spacing: 5px">Organize <br>Modern Knowledge<br> for Mankind</h2>
<p style="font-size: 20px;margin-top: 35px;letter-spacing: 4px">Consume, Colect and Revisit <br>Knowledge at Your Fingertips</p>
<a href="#" style="margin-top: 80px;display: inline-block;margin-left: -17px"><img src="../public/images/btn_get_chrome.png"></a>
</div>
</div>
</div>
</div>
{{end}}
</body>
</html>
But when I run server, and request the path on browser, it displays an error
open base: no such file or directory
Something's wrong with my rendering function?
答案1
得分: 1
t, err := template.ParseFiles(tmpl...)
我猜测 tmpl
不是一个文件路径
我猜测你想要像这样做:
files := make([]string, len(tmpl))
for i, file := range tmpl{
files[i] = filepath.Join(cwd, "./view/"+file+".html")
}
t, err := template.ParseFiles(files...)
英文:
t, err := template.ParseFiles(tmpl...)
I guess `tmpl' seems not a filepath :(<br >
I guess you wanna like this.
files := make([]string, len(tmpl))
for i, file := range tmpl{
files[i] = appendfilepath.Join(cwd,"./view/"+file+".html")
}
t, err := template.ParseFiles(files...)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论