在包含的模板中,变量无法正常工作(html/template)。

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

Variables not working in included templates (html/template)

问题

模板"head"被插入到"index"模板中,并使用一个变量{{ .Title }}

Main.go:

package main

import (
	"html/template"
	"net/http"

	"github.com/julienschmidt/httprouter"
)

var (
	t = template.Must(template.ParseGlob("templates/*.tpl"))
)

type Page struct {
	Title string
	Desc  string
}

func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	index := Page{Title: "This is title", Desc: "Desc"}
	t.ExecuteTemplate(w, "index", index)
}

func main() {
	router := httprouter.New()
	router.GET("/", Index)

	http.ListenAndServe(":8080", router)
}

Index.tpl:

{{ define "index" }}

<!DOCTYPE html>
<html lang="en">
	{{ template "head" }}
<body>
	<h1>Main info:</h1>
	Title: {{ .Title }}
	Desc: {{ .Desc }}
</body>
</html>


{{ end }}

head.tpl:

{{ define "head" }}

<head>
	<meta charset="UTF-8">
	<title>{{ .Title }}</title>
</head>

{{ end }}

我得到了这个HTML:

<!DOCTYPE html>
<html lang="en">
	

<head>
	<meta charset="UTF-8">
	<title></title>
</head>


<body>
	<h1>Main info:</h1>
	Title: This is title
	Desc: Desc
</body>
</html>

变量{{ .Title }}在网页正文中起作用,但在头部不起作用。

英文:

The template "head" inserted on "index" template and use one variable {{ .Title }}

Main.go:

package main

import (
	&quot;html/template&quot;
	&quot;net/http&quot;

	&quot;github.com/julienschmidt/httprouter&quot;
)

var (
	t = template.Must(template.ParseGlob(&quot;templates/*.tpl&quot;))
)

type Page struct {
	Title string
	Desc  string
}

func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	index := Page{Title: &quot;This is title&quot;, Desc: &quot;Desc&quot;}
	t.ExecuteTemplate(w, &quot;index&quot;, index)
}

func main() {
	router := httprouter.New()
	router.GET(&quot;/&quot;, Index)

	http.ListenAndServe(&quot;:8080&quot;, router)
}

Index.tpl:

{{ define &quot;index&quot; }}

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
	{{ template &quot;head&quot; }}
&lt;body&gt;
	&lt;h1&gt;Main info:&lt;/h1&gt;
	Title: {{ .Title }}
	Desc: {{ .Desc }}
&lt;/body&gt;
&lt;/html&gt;


{{ end }}

head.tpl:

{{ define &quot;head&quot; }}

&lt;head&gt;
	&lt;meta charset=&quot;UTF-8&quot;&gt;
	&lt;title&gt;{{ .Title }}&lt;/title&gt;
&lt;/head&gt;

{{ end }}

I get this html:

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
	

&lt;head&gt;
	&lt;meta charset=&quot;UTF-8&quot;&gt;
	&lt;title&gt;&lt;/title&gt;
&lt;/head&gt;


&lt;body&gt;
	&lt;h1&gt;Main info:&lt;/h1&gt;
	Title: This is title
	Desc: Desc
&lt;/body&gt;
&lt;/html&gt;

Variable {{ .Title }} works on site body, but doesn't work in head.

答案1

得分: 5

你必须将变量传递给模板:

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

You have to pass the variables to the template:

{{ template &quot;head&quot; . }}

huangapple
  • 本文由 发表于 2015年1月24日 16:14:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/28123595.html
匿名

发表评论

匿名网友

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

确定