打开文件:没有这样的文件或目录。

huangapple go评论126阅读模式
英文:

go - open file no such file or directory

问题

我写了一个包装函数来渲染模板,代码如下:

  1. func RenderTemplate(w http.ResponseWriter, data interface{}, tmpl... string) {
  2. cwd, _ := os.Getwd()
  3. for _ , file := range tmpl{
  4. file = filepath.Join(cwd,"./view/"+file+".html")
  5. }
  6. t, err := template.ParseFiles(tmpl...)
  7. if err != nil {
  8. http.Error(w, err.Error(), http.StatusInternalServerError)
  9. return
  10. }
  11. err = t.Execute(w, data)
  12. if err != nil {
  13. http.Error(w, err.Error(), http.StatusInternalServerError)
  14. }
  15. }

我在 main 函数中设置了静态文件夹和路由处理程序:

  1. http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("public"))))
  2. http.HandleFunc("/", route.IndexHandler)

这是我的路由处理程序:

  1. func IndexHandler(w http.ResponseWriter, r *http.Request) {
  2. files := []string{"base", "index"}
  3. util.RenderTemplate(w, nil, files...)
  4. }

我打算使用模板嵌套来帮助 index.html 扩展 base.html

这是 base.html 的内容:

  1. {{define "base"}}
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <title>{{template "title" .}}</title>
  7. <script type="text/javascript" src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
  8. <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  9. <script type="text/javascript" src="/js/isotope.pkgd.min.js"></script>
  10. <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  11. <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
  12. <link rel="stylesheet" type="text/css" href="/css/style.css">
  13. </head>
  14. <body>
  15. {{template "index" .}}
  16. </body>
  17. </html>
  18. {{end}}

这是 index.html 的内容:

  1. <!DOCTYPE html>
  2. <html>
  3. {{define "title"}}Homepage{{end}}
  4. <body>
  5. {{define "index"}}
  6. <div class="wrapper">
  7. <div class="page-content">
  8. <div class="container">
  9. <div class="left">
  10. <img src="../public/images/img_landing_page_mac.png">
  11. </div>
  12. <div class="right">
  13. <h2 style="font-size: 33px; letter-spacing: 5px">Organize <br>Modern Knowledge<br> for Mankind</h2>
  14. <p style="font-size: 20px;margin-top: 35px;letter-spacing: 4px">Consume, Colect and Revisit <br>Knowledge at Your Fingertips</p>
  15. <a href="#" style="margin-top: 80px;display: inline-block;margin-left: -17px"><img src="../public/images/btn_get_chrome.png"></a>
  16. </div>
  17. </div>
  18. </div>
  19. </div>
  20. {{end}}
  21. </body>
  22. </html>

但是当我运行服务器并在浏览器上请求路径时,显示出错信息 open base: no such file or directory。我的渲染函数有问题吗?

英文:

I write a wrapper function to render template like this

  1. func RenderTemplate(w http.ResponseWriter, data interface{}, tmpl... string) {
  2. cwd, _ := os.Getwd()
  3. for _ , file := range tmpl{
  4. file = filepath.Join(cwd,&quot;./view/&quot;+file+&quot;.html&quot;)
  5. }
  6. t, err := template.ParseFiles(tmpl...)
  7. if err != nil {
  8. http.Error(w, err.Error(), http.StatusInternalServerError)
  9. return
  10. }
  11. err = t.Execute(w, data)
  12. if err != nil {
  13. http.Error(w, err.Error(), http.StatusInternalServerError)
  14. }
  15. }

I setup static folder and route handler in main

  1. http.Handle(&quot;/public/&quot;,http.StripPrefix(&quot;/public/&quot;,http.FileServer(http.Dir(&quot;public&quot;))))
  2. http.HandleFunc(&quot;/&quot;,route.IndexHandler)

And this is my route handler

  1. func IndexHandler(w http.ResponseWriter, r *http.Request) {
  2. files:=[]string{&quot;base&quot;,&quot;index&quot;}
  3. util.RenderTemplate(w, nil, files...)
  4. }

I intend to use template nesting to help index.html extend base.html

This is base.html

  1. {{define &quot;base&quot;}}
  2. &lt;!DOCTYPE html&gt;
  3. &lt;html&gt;
  4. &lt;head&gt;
  5. &lt;meta charget=&quot;utf-8&quot;&gt;
  6. &lt;title&gt;{{template &quot;title&quot;.}}&lt;/title&gt;
  7. &lt;script type=&quot;text/javascript&quot; src=&quot;https://code.jquery.com/jquery-2.2.0.min.js&quot;&gt;&lt;/script&gt;
  8. &lt;script type=&quot;text/javascript&quot; src=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js&quot;&gt;&lt;/script&gt;
  9. &lt;script type=&quot;text/javascript&quot; src=&quot;/js/isotope.pkgd.min.js&quot;&gt;&lt;/script&gt;
  10. &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css&quot;&gt;
  11. &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css&quot;&gt;
  12. &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;/css/style.css&quot;&gt;
  13. &lt;/head&gt;
  14. &lt;body&gt;
  15. {{template &quot;index&quot;.}}
  16. &lt;/body&gt;
  17. &lt;/html&gt;
  18. {{end}}

This is index.html

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html&gt;
  3. {{define &quot;title&quot;}}Homepage{{end}}
  4. &lt;body&gt;
  5. {{define &quot;index&quot;}}
  6. &lt;div class=&quot;wrapper&quot;&gt;
  7. &lt;div class=&quot;page-content&quot;&gt;
  8. &lt;div class=&quot;container&quot;&gt;
  9. &lt;div class=&quot;left&quot;&gt;
  10. &lt;img src=&quot;../public/images/img_landing_page_mac.png&quot;&gt;
  11. &lt;/div&gt;
  12. &lt;div class=&quot;right&quot;&gt;
  13. &lt;h2 style=&quot;font-size: 33px; letter-spacing: 5px&quot;&gt;Organize &lt;br&gt;Modern Knowledge&lt;br&gt; for Mankind&lt;/h2&gt;
  14. &lt;p style=&quot;font-size: 20px;margin-top: 35px;letter-spacing: 4px&quot;&gt;Consume, Colect and Revisit &lt;br&gt;Knowledge at Your Fingertips&lt;/p&gt;
  15. &lt;a href=&quot;#&quot; style=&quot;margin-top: 80px;display: inline-block;margin-left: -17px&quot;&gt;&lt;img src=&quot;../public/images/btn_get_chrome.png&quot;&gt;&lt;/a&gt;
  16. &lt;/div&gt;
  17. &lt;/div&gt;
  18. &lt;/div&gt;
  19. &lt;/div&gt;
  20. {{end}}
  21. &lt;/body&gt;
  22. &lt;/html&gt;

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...)

英文:
  1. t, err := template.ParseFiles(tmpl...)

I guess `tmpl' seems not a filepath :(<br >
I guess you wanna like this.

  1. files := make([]string, len(tmpl))
  2. for i, file := range tmpl{
  3. files[i] = appendfilepath.Join(cwd,&quot;./view/&quot;+file+&quot;.html&quot;)
  4. }
  5. t, err := template.ParseFiles(files...)

huangapple
  • 本文由 发表于 2016年3月10日 08:22:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/35905338.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定