使用Golang模板,你可以在每个模板中设置一个变量。

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

With Golang Templates how can I set a variable in each template?

问题

如何在每个模板中设置一个变量,以便在其他模板中使用,例如:

在一个模板中设置变量:

{{ set title "Title" }}

然后在我的布局中:

<title>{{ title }}</title>

当渲染时:

tmpl, _ := template.ParseFiles("layout.html", "home.html")

它将根据在home.html中设置的内容来设置标题,而不需要为每个视图页面创建一个struct,这并不是必需的。希望我表达清楚,谢谢。

只是为了澄清:

layout.html:




{{ title }}



home.html:
{{ set Title "Home" . }}

{{ Title }} Page

英文:

How can I set a variable in each template that I can use in other templates e.g.

{{ set title &quot;Title&quot; }}

in one template then in my layout

&lt;title&gt; {{ title }} &lt;/title&gt;

Then when it's rendered

tmpl, _ := template.ParseFiles(&quot;layout.html&quot;, &quot;home.html&quot;)

it will set the title according to whatever was set in home.html instead of having to make a struct for each view page when it isn't really necessary. I hope I made sense, thanks.

Just for clarification:

layout.html:
&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;{{ title }} &lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;

  &lt;/body&gt;
&lt;/html&gt;

home.html:
{{ set Title &quot;Home&quot; . }}
&lt;h1&gt; {{ Title }} Page &lt;/h1&gt;

答案1

得分: 8

如果你想在另一个模板中使用该值,可以将其传递给点操作符:

{{with $title := "SomeTitle"}}
{{$title}} <-- 在页面上打印该值
{{template "body" .}}
{{end}}

body模板:

{{define "body"}}
<h1>{{.}}</h1> <-- 再次打印 "SomeTitle"
{{end}}

据我所知,不可能向上返回到链中的上一级。所以layout.htmlhome.html之前被渲染,所以你不能传递一个值回去。

在你的例子中,最好的解决方案是使用一个结构体,并使用dot将其从layout.html传递到home.html

main.go

package main

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

type WebData struct {
    Title string
}

func homeHandler(w http.ResponseWriter, r *http.Request) {
    tmpl, _ := template.ParseFiles("layout.html", "home.html")
    wd := WebData{
        Title: "Home",
    }
    tmpl.Execute(w, &wd)
}

func pageHandler(w http.ResponseWriter, r *http.Request) {
    tmpl, _ := template.ParseFiles("layout.html", "page.html")
    wd := WebData{
        Title: "Page",
    }
    tmpl.Execute(w, &wd)
}

func main() {
    http.HandleFunc("/home", homeHandler)
    http.HandleFunc("/page", pageHandler)
    http.ListenAndServe(":8080", nil)
}

layout.html

<!DOCTYPE html>
<html>
  <head>
    <title>{{.Title}} </title>
  </head>
  <body>
    {{template "body" .}}
  </body>
</html>

home.html

{{define "body"}}
<h1>home.html {{.Title}}</h1>
{{end}}

page.html

{{define "body"}}
<h1>page.html {{.Title}}</h1>
{{end}}

此外,Go语言有一个很好的关于如何使用模板的文档:

http://golang.org/pkg/text/template/

英文:

If you want to use the Value in another template you can pipeline it to the dot:

{{with $title := &quot;SomeTitle&quot;}}
{{$title}} &lt;--prints the value on the page
{{template &quot;body&quot; .}}
{{end}}

body template:

{{define &quot;body&quot;}}
&lt;h1&gt;{{.}}&lt;/h1&gt; &lt;--prints &quot;SomeTitle&quot; again
{{end}}

As far as i know it is not possible to go upwards in the chain.
So layout.html gets rendered before home.html, so you cant pass a value back.

In your example it would be the best solution to use a struct and pass it from the layout.html to the home.html using the dot:

main.go

package main

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

type WebData struct {
    Title string
}

func homeHandler(w http.ResponseWriter, r *http.Request) {
    tmpl, _ := template.ParseFiles(&quot;layout.html&quot;, &quot;home.html&quot;)
    wd := WebData{
        Title: &quot;Home&quot;,
    }
    tmpl.Execute(w, &amp;wd)
}

func pageHandler(w http.ResponseWriter, r *http.Request) {
    tmpl, _ := template.ParseFiles(&quot;layout.html&quot;, &quot;page.html&quot;)
    wd := WebData{
        Title: &quot;Page&quot;,
    }
    tmpl.Execute(w, &amp;wd)
}

func main() {
    http.HandleFunc(&quot;/home&quot;, homeHandler)
    http.HandleFunc(&quot;/page&quot;, pageHandler)
    http.ListenAndServe(&quot;:8080&quot;, nil)
}

layout.html

&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;{{.Title}} &lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    {{template &quot;body&quot; .}}
  &lt;/body&gt;
&lt;/html&gt;

home.html

{{define &quot;body&quot;}}
&lt;h1&gt;home.html {{.Title}}&lt;/h1&gt;
{{end}}

page.html

{{define &quot;body&quot;}}
&lt;h1&gt;page.html {{.Title}}&lt;/h1&gt;
{{end}}

Also go has a nice documentation on how to use templates:

http://golang.org/pkg/text/template/

huangapple
  • 本文由 发表于 2015年6月3日 09:45:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/30609485.html
匿名

发表评论

匿名网友

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

确定