英文:
Rendering template in golang
问题
我正在使用Go语言中的echo框架创建一个Web应用程序。我有一个名为templates
的目录,里面有两个目录layouts
和users
。目录结构如下:
layouts
|--------default.tmpl
|--------footer.tmpl
|--------header.tmpl
|--------sidebar.tmpl
users
|--------index.tmpl
header、footer和sidebar的代码相似,如下所示:
{{define "header"}}
<!-- 这里是一些HTML代码 -->
{{ end }}
....
default.tmpl
的内容如下:
{{ define "default" }}
{{ template "header" }}
{{ template "sidebar" }}
<div class="content-wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<h2 class="page-title">Dashboard</h2>
{{ template "content" .}}
</div>
</div>
</div>
</div>
{{ template "footer" }}
{{ end }}
而users\index.tmpl
的内容如下:
{{define "index"}}
{{template "default"}}
{{end}}
{{define "content"}}
<p>Hello world</p>
{{end}}
现在,我使用以下代码解析这些文件:
t := &Template{}
t.templates = template.Must(template.ParseGlob("views/layouts/*"))
t.templates = template.Must(template.ParseGlob("views/user/*"))
然后尝试渲染它:
func User(c echo.Context) error {
return c.Render(http.StatusOK, "index", nil)
}
但是我只得到一个内部服务器错误。我也不知道如何调试模板。如果users\index.tmpl
中不包含其他模板标签,代码是可以正常工作的。但是当我尝试在其中包含主模板时,错误就会出现。我在这里做错了什么?
英文:
I am using the echo framework in Go to create a web app. I have a directory called templates
inside which I have two directories layouts
and users
. The directory tree is as follows:
layouts
|--------default.tmpl
|--------footer.tmpl
|--------header.tmpl
|--------sidebar.tmpl
users
|--------index.tmpl
The code for header, footer, and sidebar are similar with:
{{define "header"}}
<!-- some html here -->
{{ end }}
....
default.tmpl
is as follows:
{{ define "default" }}
{{ template "header" }}
{{ template "sidebar" }}
<div class="content-wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<h2 class="page-title">Dashboard</h2>
{{ template "content" .}}
</div>
</div>
</div>
</div>
{{ template "footer" }}
{{ end }}
And users\index.tmpl
{{define "index"}}
{{template "default"}}
{{end}}
{{define "content"}}
<p>Hello world</p>
{{end}}
Now, I parse the files using
t := &Template{}
t.templates = template.Must(template.ParseGlob("views/layouts/*"))
t.templates = template.Must(template.ParseGlob("views/user/*"))
And try to render it
func User(c echo.Context) error {
return c.Render(http.StatusOK, "index", nil)
}
But I only get an internal server error. I don't know how to debug the templates either. The code works if the users\index.tmpl
does not contain other template tags inside it. But when I try to include the main template in it, the error returns. What am I doing wrong here?
答案1
得分: 2
成功解决了。这个页面https://elithrar.github.io/article/approximating-html-template-inheritance/有所帮助。
基本上,我需要改变解析模板的代码:
tpls, err := filepath.Glob("views/user/*")
if err != nil {
log.Fatal(err)
}
layouts, err := filepath.Glob("views/layouts/*")
if err != nil {
log.Fatal(err)
}
for _, layout := range layouts {
files := append(layouts, tpls)
t.templates = template.Must(template.ParseFiles(files...))
}
英文:
Managed to solve this. This page https://elithrar.github.io/article/approximating-html-template-inheritance/ helped.
Basically, I had to change the code with which I parsed the templates to:
tpls, err := filepath.Glob("views/user/*")
if err != nil {
log.Fatal(err)
}
layouts, err := filepath.Glob("views/layouts/*")
if err != nil {
log.Fatal(err)
}
for _, layout := range layouts {
files := append(layouts, tpls)
t.templates = template.Must(template.ParseFiles(files...))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论