【问题标题】:Template renders nothing, and no error, but status is 200模板什么也不渲染,也没有错误,但状态是 200
【发布时间】:2019-12-05 11:27:54
【问题描述】:

我在一个简单的 HTTP 服务器上玩 Go:

// var tpl = template.Must(template.New("").Funcs(template.FuncMap{"isRegistered": isRegistered}).ParseGlob("templates/*")) // functions will be added later
var tpl = template.Must(template.ParseGlob("templates/*"))

func contact(w http.ResponseWriter, r *http.Request) {
    //// defined templates are: "home.html", "layout", "layout.html", "contact.html", "body"
    log.Println("in handler: ", tpl.DefinedTemplates())

    err := tpl.ExecuteTemplate(w, "contact.html", nil)
    if err != nil {
        fmt.Println(err) // no error displayed
    }
    // fmt.Fprintf((w), "write") - This works fine
}

func main() {

    log.Println("Serving on 8888 port")
    http.HandleFunc("/contact", contact)

    http.ListenAndServe(":8888", nil)
}


{{define "layout"}}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{.Title}}</title>
    <meta name="description" content="{{.Description}}">
    <link rel="canonical" href="{{.Canonical}}" />
</head>
<body>

{{template "body" .}}

</body>
</html>
{{end}}

{{define "body"}}
<h1>Contact us page</h1>

<p>
    Your name is...
</p>

{{end}}

localhost:8888/contact 返回 OK 200 和空正文。 我用了这个例子:https://stackoverflow.com/a/36643663/2110953

但我以后还需要添加模板函数: var tpl = template.Must(template.New("").Funcs(template.FuncMap{"isRegistered": isRegistered}).ParseGlob("templates/*"))

【问题讨论】:

  • 您没有在控制台上看到任何错误? contact.html 模板长什么样子?
  • 没有错误,这就是为什么要问...
  • 请注意,Go 模板永远不会返回 200。他们完全不知道 HTTP 状态代码。
  • 您必须在控制台中有一些输出。
  • 谢谢,我知道,我的意思是完全没有错误,你看我的代码,我用模板更新了。

标签: templates go go-templates


【解决方案1】:

您的contact.html 不会“渲染”任何内容。它只定义了body 模板,但不包含它(执行它)。

要执行模板(在模板内),您可以使用{{template}} 操作。要定义执行模板,您可以使用{{block}} 操作。

Template Actions:

{{template "name"}}
    The template with the specified name is executed with nil data.

{{template "name" pipeline}}
    The template with the specified name is executed with dot set
    to the value of the pipeline.

{{block "name" pipeline}} T1 {{end}}
    A block is shorthand for defining a template
        {{define "name"}} T1 {{end}}
    and then executing it in place
        {{template "name" pipeline}}
    The typical use is to define a set of root templates that are
    then customized by redefining the block templates within.

如果您的目标是在所有页面中都有一个“固定”的页眉和页脚,那么您必须重新构建您的模板。在某处定义了headerfooter 模板,页面应将它们作为第一个和最后一个元素包含在内。见How to use a field of struct or variable value as template name?

【讨论】:

  • 谢谢,但我不明白如果要修复它应该怎么做,只是无法理解它,我知道我应该以某种方式传递 layout.html 和处理程序的模板(联系.html),但找不到合适的示例
  • @DoctorP 你看不到任何东西,因为你的contact.html 没有渲染任何东西,就像它是一个空文件一样。尝试用{{block "body" .}} 替换{{define "body"}},看看会发生什么。
  • 我想在渲染输出中也包含布局。 stackoverflow.com/questions/36617949/… 块仅呈现当前模板
  • @DoctorP 然后你必须重组你的模板。有一个headerfooter 模板,页面应该将它们作为第一个和最后一个元素包含在内。见How to use a field of struct or variable value as template name?
【解决方案2】:

更新:所以我只需要创建页眉和页脚模板:

{{template "header" .}}

<h1>Contact us page</h1>

<p>
    Your name is...
</p>


{{template "footer" .}}
{{define "header"}}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{.Title}}</title>
    <meta name="description" content="{{.Description}}">
    <link rel="canonical" href="{{.Canonical}}" />
</head>
<body>
{{end}}
{{define "footer"}}
</body>
</html>
{{end}}

效果很好

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-13
    • 2012-09-10
    • 2020-11-05
    • 2016-04-23
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    相关资源
    最近更新 更多