go模板在使用2个ParseFiles时没有接收到传递的数据。

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

go template is not receiving data passed when using 2 ParseFiles

问题

我正在尝试创建一个处理程序,然后编译两个模板:template.html 用于布局目的,config.html 是实际页面。

这段代码构建了页面,但没有传递数据:

func config(w http.ResponseWriter, r *http.Request) {
    fpTemplate := filepath.Join("static", "template.html")
    fpPage := filepath.Join("static", "config.html")

    tmpl, err := template.ParseFiles(fpPage, fpTemplate)
    if err != nil {
        log.Println("webserver.config: " + err.Error())
    }

    vd := ViewData{&Settings}

    err = tmpl.ExecuteTemplate(w, "template.html", vd)
    if err != nil {
        log.Println("webserver.config: " + err.Error())
    }
}

config.html 的内容如下:

{{define "title"}} 
Config 
{{end}}

{{define "body"}}

<p class="text-break">

{{ .}}

</p>
{{end}}

当我运行以下代码时:

func config(w http.ResponseWriter, r *http.Request) {
    fpTemplate := filepath.Join("static", "template.html")
    fpPage := filepath.Join("static", "config.html")

    //tmpl, err := template.ParseFiles(fpPage, fpTemplate)
    tmpl, err := template.New("config.html").ParseFiles(fpPage, fpTemplate)
    if err != nil {
        log.Println("webserver.config: " + err.Error())
    }

    vd := ViewData{&Settings}

    err = tmpl.ExecuteTemplate(w, tmpl.Name(), vd)
    fmt.Println(err)
    if err != nil {
        log.Println("webserver.config: " + err.Error())
    }
}

我得到错误信息:template: no template "config.html" associated with template "config.html",并且页面是空白的。

我在这里漏掉了什么?

非常感谢任何帮助!

英文:

Im trying to create a handler which then will compile 2 templates:
template.html which serves layout purposes and the actual page: config.html.

this code builds the page, but no data is passed:

func config(w http.ResponseWriter, r *http.Request) {
	fpTemplate := filepath.Join(&quot;static&quot;, &quot;template.html&quot;)
	fpPage := filepath.Join(&quot;static&quot;, &quot;config.html&quot;)

	tmpl, err := template.ParseFiles(fpPage, fpTemplate)
	if err != nil {
		log.Println(&quot;webserver.config: &quot; + err.Error())
	}

	vd := ViewData{&amp;Settings}

	err = tmpl.ExecuteTemplate(w, &quot;template.html&quot;, vd)
	if err != nil {
		log.Println(&quot;webserver.config: &quot; + err.Error())
	}
}

and config.html like this:

{{define &quot;title&quot;}} 
Config 
{{end}}

{{define &quot;body&quot;}}

&lt;p class=&quot;text-break&quot;&gt;

{{ .}}

&lt;/p&gt;
{{end}}

, when I run this code:

func config(w http.ResponseWriter, r *http.Request) {
	fpTemplate := filepath.Join(&quot;static&quot;, &quot;template.html&quot;)
	fpPage := filepath.Join(&quot;static&quot;, &quot;config.html&quot;)

	//tmpl, err := template.ParseFiles(fpPage, fpTemplate)
	tmpl, err := template.New(&quot;config.html&quot;).ParseFiles(fpPage, fpTemplate)
	if err != nil {
		log.Println(&quot;webserver.config: &quot; + err.Error())
	}

	vd := ViewData{&amp;Settings}

	err = tmpl.ExecuteTemplate(w, tmpl.Name(), vd)
	fmt.Println(err)
	//err = tmpl.ExecuteTemplate(w, &quot;template.html&quot;, vd)
	if err != nil {
		log.Println(&quot;webserver.config: &quot; + err.Error())
	}
}

I get error: template: no template "config.html" associated with template "config.html" and blank black page.

What im I missing here ?

Appreciated any help!

答案1

得分: 1

当你在第一个代码中将"vd"传递给ExecuteTemplate时,数据将传递给主模板,当你在"template.html"中调用它时,你必须将数据传递给"body"模板,如下所示:

{{ template "body" . }}
英文:

When you pass "vd" to ExecuteTemplate in first code, the data pass to main template and you must pass the data into "body" template when you called it on "template.html" like as:

{{ template &quot;body&quot; . }}

huangapple
  • 本文由 发表于 2022年7月23日 02:03:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/73084412.html
匿名

发表评论

匿名网友

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

确定