gotemplate未渲染数据。

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

gotemplate not rendering data

问题

func renderTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
    layout := "static/layout.html"
    tmplFiles := []string{layout, tmpl}
    fmt.Printf("%v\n", data)
    t, err := template.ParseFiles(tmplFiles...)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    err = t.ExecuteTemplate(w, "layout.html", data)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
}

func main(){
    http.HandleFunc("/directors", directorsHandler)

    fs := http.FileServer(http.Dir("static"))
    http.Handle("/static/", http.StripPrefix("/static/", fs))

    fmt.Println("Server listening on port 8080...")
    http.ListenAndServe(":8080", nil)
}

directors.html
{{ define "content" }}
    Directors ----
    <h1>{{ .Title }}</h1>
    <ul>
        {{ range .Directors }}
        hi
        <li>
            <p><strong>Name:</strong> {{ .Name }}</p>
            <p><strong>Date of Birth:</strong> {{ .DateOfBirth }}</p>
            <p><strong>Nationality:</strong> {{ .Nationality }}</p>
        </li>
        {{ end }}
    </ul>
{{ end }}

func directorsHandler(w http.ResponseWriter, r *http.Request) {
    data := struct {
        Title     string
        Directors []struct {
            Name        string
            DateOfBirth string
            Nationality string
        }
    }{
        Title: "Director List",
        Directors: []struct {
            Name        string
            DateOfBirth string
            Nationality string
        }{
            {
                Name:        "Christopher Nolan",
                DateOfBirth: "July 30, 1970",
                Nationality: "British",
            },
            {
                Name:        "Quentin Tarantino",
                DateOfBirth: "March 27, 1963",
                Nationality: "American",
            },
            // Add more directors as needed
        },
    }
    renderTemplate(w, "static/directors.html", data)
}

不知道为什么 .Title 没有被渲染出来。
我怀疑是因为接口的原因,但没有找到相关的信息。
我使用匿名结构体将不同的数据发送到不同的页面。
我尝试将信息发送到 directors.html 模板的内容,但它不会被渲染出来。
更多细节:
数据被发送到 data interface{}

英文:
func renderTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
layout := &quot;static/layout.html&quot;
tmplFiles := []string{layout, tmpl}
fmt.Printf(&quot;%v\n&quot;, data)
t, err := template.ParseFiles(tmplFiles...)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = t.ExecuteTemplate(w, &quot;layout.html&quot;, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func main(){
http.HandleFunc(&quot;/directors&quot;, directorsHandler)
fs := http.FileServer(http.Dir(&quot;static&quot;))
http.Handle(&quot;/static/&quot;, http.StripPrefix(&quot;/static/&quot;, fs))
fmt.Println(&quot;Server listening on port 8080...&quot;)
http.ListenAndServe(&quot;:8080&quot;, nil)
}
directors.html
{{ define &quot;content&quot; }}
Directors ----
&lt;h1&gt;{{ .Title }}&lt;/h1&gt;
&lt;ul&gt;
{{ range .Directors }}
hi
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Name:&lt;/strong&gt; {{ .Name }}&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Date of Birth:&lt;/strong&gt; {{ .DateOfBirth }}&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Nationality:&lt;/strong&gt; {{ .Nationality }}&lt;/p&gt;
&lt;/li&gt;
{{ end }}
&lt;/ul&gt;
{{ end }}
func directorsHandler(w http.ResponseWriter, r *http.Request) {
data := struct {
Title     string
Directors []struct {
Name        string
DateOfBirth string
Nationality string
}
}{
Title: &quot;Director List&quot;,
Directors: []struct {
Name        string
DateOfBirth string
Nationality string
}{
{
Name:        &quot;Christopher Nolan&quot;,
DateOfBirth: &quot;July 30, 1970&quot;,
Nationality: &quot;British&quot;,
},
{
Name:        &quot;Quentin Tarantino&quot;,
DateOfBirth: &quot;March 27, 1963&quot;,
Nationality: &quot;American&quot;,
},
// Add more directors as needed
},
}
renderTemplate(w, &quot;static/directors.html&quot;, data)
}

don't know why .Title is not being rendered.
i have the suspicion that its because the interface but didn't find anything about it.
i am using anonymous struct to send different data to different pages.
I tried to send information to directors.html template content but it wont render.
more details:
the data is being send to data interface{} .

答案1

得分: 1

在发送数据到具有布局的页面时发现问题,你需要写成{{template "content" .}}而不是{{ template "content" }}。需要添加一个点。

英文:

Problem found when sending data to a page with layout you need to write {{template "content" .}} instead of {{ template "content" }}.
needed to add a dot.

huangapple
  • 本文由 发表于 2023年6月25日 07:19:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76548314.html
匿名

发表评论

匿名网友

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

确定