I have two structs as below and I need to render the data on a template using the templates pack. I get this error
<.Email>: Email is not a field of struct type Notes
.
The issue seems to be that only fields of the range struct seem to be available within the range loop so I'm wondering how I can import fields from outside the range struct (e.g. the Email string).
The behavior is quite unexpected.
type notes struct{
Note string
sf string
}
type uis struct{
notes []Note
Email string
}
var ui uis
HTML
{{range .notes}}
{{.Email}} {{.sf}}
{{end}}
Email {{.Email}}
I've checked the godocs but they seem quite useless.
From the documentation:
When execution begins, $ is set to the data argument passed to Execute, that is, to the starting value of dot.
Therefore, you can use this:
{{range .notes}}
{{$.Email}} {{.sf}}
{{end}}
Email {{.Email}}
(Note the dollar sign inside the range)
Playground link: http://play.golang.org/p/XiQFcGJEyR
Side note: Next time try to provide proper code and a better explanation. As it stands, I think I've answered this, but I cannot be sure. Your code doesn't compile - for example, type names are wrong/mixed with members and you have unexported fields so they cannot be accessed by the templates.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With