英文:
beego template value range executing "content" not a field of struct type
问题
在我的控制器中,我设置了以下数据:
c.Data["foos"] = foos
和
c.Data["user"] = user
所以,如果我在视图中询问用户的某个属性,一切都正常。
{{if .user.IsSuperUser}}
<th>ID</th>
<th>Username</th>
{{end}}
但是在:
<tbody>
{{range $foo := .foos}}
<tr>
{{if .user.IsSuperUser}}
<td>xyz</td>
<td>abc</td>
{{end}}
...
myBeego:template: foo/foos.tpl:56:46: 执行"content"时出错
<.user.IsSuperUser>: user不是*models.Foo结构体类型的字段
我该如何处理这个问题?
感谢您的帮助,祝感恩节快乐。
英文:
in my controller i set the following data:
c.Data["foos"] = foos
and
c.Data["user"] = user
So if I ask some property from user in the view, all fine.
{{if .user.IsSuperUser}}
<th>ID</th>
<th>Username</th>
{{end}}
But in:
<tbody>
{{range $foo := .foos}}
<tr>
{{if .user.IsSuperUser}}
<td>xyz</td>
<td>abc</td>
{{end}}
...
> myBeego:template: foo/foos.tpl:56:46: executing "content" at
> <.user.IsSuperUser>: user is not a field of struct type *models.Foo
How can I handle that?
Thanks for every help and happy Thanksgiving.
答案1
得分: 3
我找到了这个不错的链接:https://stackoverflow.com/questions/14800204/in-a-template-how-do-you-access-an-outer-scope-while-inside-of-a-with-or-rang?rq=1
{{with .Inner}}
Outer: {{$.OuterValue}}
Inner: {{.InnerValue}}
{{end}}
就是这样。
英文:
I found this nice little link: https://stackoverflow.com/questions/14800204/in-a-template-how-do-you-access-an-outer-scope-while-inside-of-a-with-or-rang?rq=1
{{with .Inner}}
Outer: {{$.OuterValue}}
Inner: {{.InnerValue}}
{{end}}
That's it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论