英文:
Go template: can't evaluate field X in type Y (X not part of Y but stuck in a {{range}} loop)
问题
类似的问题在这里得到了解答,但我不认为它解决了我的问题。
假设你有以下结构体:
type User struct {
Username string
Password []byte
Email string
...
}
此外,URL 的结构如下:example.com/en/users
,其中 "en"
是一个 URL 参数,将被传递到模板中,如下所示:
renderer.HTML(w, http.StatusOK, "users/index", map[string]interface{}{
"lang": chi.URLParam(r, "lang"),
"users": users})
在 HTML 模板中,我有以下内容:
{{ range .users }}
<form action="/{{ .lang }}/users" method="POST">
<input type="text" name="Username" value="{{ .Username }}">
<input type="text" name="Email" value="{{ .Email }}">
</form>
{{ end }}
现在的问题是,因为 {{ .lang }}
不是 User
结构体的一部分,所以我会得到错误。那么,我如何在 {{ range .users }}
内部访问 {{ .lang }}
?
英文:
Similar question answered here, but I don't think it solves my problem.
Let's say you have the following struct:
type User struct {
Username string
Password []byte
Email string
...
}
Moreover, the URL hasa structure like this: example.com/en/users
, where "en"
is a URL param that will be passed into the template like this:
renderer.HTML(w, http.StatusOK, "users/index", map[string]interface{}{
"lang": chi.URLParam(r, "lang"),
"users": users})
And in the HTML template I have the following:
{{ range .users }}
<form action="/{{ .lang }}/users" method="POST">
<input type="text" name="Username" value="{{ .Username }}">
<input type="text" name="Email" value="{{ .Email }}">
</form>
{{ end }}
Now, the problem is that because {{ .lang }}
is not a part of the User
struct then I get the error.. so how can I access {{ .lang }}
inside {{ range .users }}
?
答案1
得分: 50
在调用range
之后,点号(.
)的内容被赋值给$
,因此您可以使用$
来访问lang
(在play中查看):
{{ range .users }}
<form action="/{{ $.lang }}/users" method="POST">
<input type="text" name="Username" value="{{ .Username }}">
<input type="text" name="Email" value="{{ .Email }}">
</form>
{{ end }}
该行为在这里有文档记录:
当执行开始时,
$
被设置为传递给Execute
的数据参数,也就是点号的初始值。
如果您正在使用嵌套的range
,您可以始终使用with
语句或变量赋值语句将点号赋值给其他变量。请参阅另一个答案。
英文:
The contents of dot (.
) are assigned to $
after invocation of range
, so you can use $
to access lang
(on play):
{{ range .users }}
<form action="/{{ $.lang }}/users" method="POST">
<input type="text" name="Username" value="{{ .Username }}">
<input type="text" name="Email" value="{{ .Email }}">
</form>
{{ end }}
The behavior is documented here:
> When execution begins, $
is set to the data argument passed to Execute
, that is, to the starting value of dot.
If you are using nested ranges, you can always fall back to assign dot to something else using the with
statement or variable assignment statements. See the other answer for that.
答案2
得分: 11
你可以使用一个变量来表示.lang
。
{{ $lang := .lang }}
{{ range .users }}
<form action="/{{ $lang }}/users" method="POST">
<input type="text" name="Username" value="{{ .Username }}">
<input type="text" name="Email" value="{{ .Email }}">
</form>
{{ end }}
在这里查看文档:https://golang.org/pkg/text/template/#hdr-Variables
英文:
You can use a variable for .lang
{{ $lang := .lang }}
{{ range .users }}
<form action="/{{ $lang }}/users" method="POST">
<input type="text" name="Username" value="{{ .Username }}">
<input type="text" name="Email" value="{{ .Email }}">
</form>
{{ end }}
See here at the documentation: https://golang.org/pkg/text/template/#hdr-Variables
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论