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

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

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,&quot;./view/&quot;+file+&quot;.html&quot;)
    }
    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(&quot;/public/&quot;,http.StripPrefix(&quot;/public/&quot;,http.FileServer(http.Dir(&quot;public&quot;))))

http.HandleFunc(&quot;/&quot;,route.IndexHandler)

And this is my route handler

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

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

This is base.html

{{define &quot;base&quot;}}
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
	&lt;meta charget=&quot;utf-8&quot;&gt;
	&lt;title&gt;{{template &quot;title&quot;.}}&lt;/title&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;https://code.jquery.com/jquery-2.2.0.min.js&quot;&gt;&lt;/script&gt;
	&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;
	&lt;script type=&quot;text/javascript&quot; src=&quot;/js/isotope.pkgd.min.js&quot;&gt;&lt;/script&gt;
	&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;
	&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;
	&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;/css/style.css&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
	{{template &quot;index&quot;.}}
&lt;/body&gt;
&lt;/html&gt;
{{end}}

This is index.html

&lt;!DOCTYPE html&gt;
&lt;html&gt;
{{define &quot;title&quot;}}Homepage{{end}}
&lt;body&gt;
	{{define &quot;index&quot;}}
	&lt;div class=&quot;wrapper&quot;&gt;
		&lt;div class=&quot;page-content&quot;&gt;
			&lt;div class=&quot;container&quot;&gt;
				&lt;div class=&quot;left&quot;&gt;
					&lt;img src=&quot;../public/images/img_landing_page_mac.png&quot;&gt;
				&lt;/div&gt;
				&lt;div class=&quot;right&quot;&gt;
					&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;
					&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;
					&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;
				&lt;/div&gt;
			&lt;/div&gt;
		&lt;/div&gt;
	&lt;/div&gt;
	{{end}}
&lt;/body&gt;
&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...)

英文:
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,&quot;./view/&quot;+file+&quot;.html&quot;)
}
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:

确定