can't access to data in template

984 views
Skip to first unread message

myal...@gmail.com

unread,
Mar 6, 2015, 3:18:34 AM3/6/15
to golan...@googlegroups.com
hi all gophers
please help me
i write above code : 

app.go :
package main

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

var tmpl = template.Must(template.ParseGlob("static/*.html"))

func CheckErr(err error) {
if err != nil {
fmt.Println(err)
}
}

type Data struct {
Member
Tools
}

type Member struct {
Name,Family,Username string
}

type Tools struct {
Title string
}

func Root(w http.ResponseWriter,r *http.Request) {
d := Data{}
var result []Data
d.Member.Name = "alex"
d.Member.Family = "alexfamily"
d.Member.Username = "myalexer"
d.Tools.Title = "This is the title"
result = append(result,d)
err := tmpl.ExecuteTemplate(w,"root",result)
CheckErr(err)
}

func main() {
http.HandleFunc("/",Root)
http.ListenAndServe(":4040",nil)
}



root.html :
{{define "root"}}
<!DOCTYPE html>
<html>
<head>
<title>{{.Tools.Title}}</title>
</head>
<body>
<table>
<tr>
<th>name</th>
<th>family</th>
<th>username</th>
</tr>
{{range .}}
<tr>
<td>{{.Member.Name}}</td>
<td>{{.Member.Family}}</td>
<td>{{.Member.Username}}</td>
</tr>
{{end}}
</table>
</body>
</html>
{{end}}


i have a problem with fetch Title from Tools structure
when i run program and call http://localhost:4040/ it return error : 
template : root.html:5:17: executing "root" at <.Tools.Title>: can't evaluate field Tools in type []main.Data

how do i do ?

Val

unread,
Mar 6, 2015, 7:15:55 PM3/6/15
to golan...@googlegroups.com
Indeed, Data and []Data are two different types. The slice type doesn't have a field "Tools".

First try removing the <title> tag to fix the crash. Then, think how to design some PageFacade struct which would contain a HTML title, other page fields, and a slice of Data.

myal...@gmail.com

unread,
Mar 7, 2015, 12:50:35 AM3/7/15
to golan...@googlegroups.com
i don't know how do i do ! 
please give me a example 
Message has been deleted
Message has been deleted

Erfan Akbarimanesh

unread,
Mar 7, 2015, 3:39:48 AM3/7/15
to golan...@googlegroups.com

Hi

Change your structure to :

type Data struct {

      Member

      Title string

     

}

 

type Member struct {

      Name,Family,Username string

}

 


After change struct we must change app.go at this line:

 

d.Tools.Title = "This is the title"

 

Change it to :

d.Title = "This is the title"

 

Finally you can use range for fetch title or any way:

 

<head>

       <title>{{range .}}{{.Title}}{{end}}</title>

</head>

 

Other gophers in this group can provide other way to us

 

I hope my answer will help you

Good Luck 

Egon

unread,
Mar 7, 2015, 6:26:57 AM3/7/15
to golan...@googlegroups.com
First step, relax.
Second step, take a step back.

Your current, main problem isn't templating, but rather how you have approached the problem. Start with something simpler like: http://play.golang.org/p/OIY9p3N21A

Then incrementally start adding new fields and nesting, that way you can make sure that after adding something it still works. It also helps you understand better how the template package works.

For templates, the printf "%#v" is very helpful for understanding what the current context is and what fields are available.

PS: use play.golang.org for showing your problems in the future.

+ Egon
Reply all
Reply to author
Forward
0 new messages