在golang中渲染模板

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

Rendering template in golang

问题

我正在使用Go语言中的echo框架创建一个Web应用程序。我有一个名为templates的目录,里面有两个目录layoutsusers。目录结构如下:

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 &quot;header&quot;}}
&lt;!-- some html here --&gt;
{{ end }} 
....

default.tmpl is as follows:

{{ define &quot;default&quot; }}
{{ template &quot;header&quot; }}

{{ template &quot;sidebar&quot; }}

&lt;div class=&quot;content-wrapper&quot;&gt;
    &lt;div class=&quot;container-fluid&quot;&gt;

        &lt;div class=&quot;row&quot;&gt;
            &lt;div class=&quot;col-md-12&quot;&gt;
                &lt;h2 class=&quot;page-title&quot;&gt;Dashboard&lt;/h2&gt;
                {{ template &quot;content&quot; .}}
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

{{ template &quot;footer&quot; }}
{{ end }}

And users\index.tmpl

{{define &quot;index&quot;}}
    {{template &quot;default&quot;}}
{{end}}

{{define &quot;content&quot;}}
&lt;p&gt;Hello world&lt;/p&gt;
{{end}}

Now, I parse the files using

t := &amp;Template{}
t.templates = template.Must(template.ParseGlob(&quot;views/layouts/*&quot;))
t.templates = template.Must(template.ParseGlob(&quot;views/user/*&quot;))

And try to render it

func User(c echo.Context) error {
	return c.Render(http.StatusOK, &quot;index&quot;, 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(&quot;views/user/*&quot;)
if err != nil {
    log.Fatal(err)
}

layouts, err := filepath.Glob(&quot;views/layouts/*&quot;)
if err != nil {
    log.Fatal(err)
}

for _, layout := range layouts {
    files := append(layouts, tpls)
    t.templates = template.Must(template.ParseFiles(files...))
}

huangapple
  • 本文由 发表于 2016年4月19日 14:48:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/36710562.html
匿名

发表评论

匿名网友

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

确定