包含其他目录中的模板

时间:2018-05-11 00:36:08

标签: go

templates \ index.gohtml 中我使用此代码:

{{template "header"}}
<h1>INDEX</h1>
{{template "nav"}}

<form action="/apply" method="post">
    <label for="firstname">First Name</label>
    <input type="text" name="firstname" id="firstname">
    <input type="submit">
</form>

但我收到错误:

  

html / template:index.gohtml:3:11:没有这样的模板“nav”

我想,这是因为nav templates \ includes \ nav.gohtml 中定义。如果是这样,我不知道为什么我没有得到header的相同错误,因为它位于同一目录中。

我的main.go看起来像这样:

func init() {
    tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
}

func main() {
    http.HandleFunc("/", index)
}

func index(w http.ResponseWriter, r *http.Request) {
    err := tpl.ExecuteTemplate(w, "index.gohtml", nil)
    if err != nil {
        log.Println(err)
        http.Error(w, "Internal server error", http.StatusInternalServerError)
    }
}

任何帮助都将不胜感激。

0 个答案:

没有答案
相关问题