英文:
How to run two anonymous struct in the same file?
问题
在这段代码中,我有两个消息。
1- 第一个是signupsuccess,表示您的账户已创建。现在登录到账户中。
2- 第二个是loginfailure,如果电子邮件或密码不匹配,则显示错误消息。
我在login.html
文件中调用了这两个消息,但是当我运行这段代码时,它只运行第一个消息,即
<div class="loginfailure">
<h1>{{.Loginfailure}}</h1>
</div>
它不允许第二个消息,如果调用第二个消息,它会显示空白页面。
handler.go
注册成功
success := struct{ Signupsuccess string }{Signupsuccess: "您的账户已成功创建"}
loginTmpl.Execute(w, success)
登录失败
failure := struct{ Loginfailure string }{Loginfailure: "请输入正确的电子邮件或密码"}
loginTmpl.Execute(w, failure)
login.html
{{define "body"}}
<div class="loginfailure">
<h1>{{.Loginfailure}}</h1>
</div>
<div class="signupsuccess">
<h1>{{.Signupsuccess}}</h1>
</div>
<h1>登录</h1>
<p>登录以访问您的账户</p>
<form action="/login" method="POST">
<div>
<label for="email">电子邮件</label>
<input type="email" name="email" placeholder="输入您的电子邮件地址" required>
</div>
<div>
<label for="password">密码</label>
<input type="password" name="password" placeholder="输入您的密码" required>
</div>
<div>
<input type="submit" value="登录">
</div>
<div>
<a href="/signup" class="link">注册</a>
</div>
</form>
{{end}}
英文:
In this code, I have two messages.
1- First one is signupsuccess that your account is created. Now login to an account and
2- The second is loginfailure that is if an email or password does not match then give an error message.
I have called both messages in the login.html
file but when I run this code, it prefers to run only the first message that is
<div class="loginfailure">
<h1>{{.Loginfailure}}</h1>
</div>
It does not allow the second one and if the second message is called, it gives the blank page.
handler.go
Signup success
success := struct{ Signupsuccess string }{Signupsuccess: "Your account is successfully created"}
loginTmpl.Execute(w, success)
Login failure
failure := struct{ Loginfailure string }{Loginfailure: "Enter the correct email or password"}
loginTmpl.Execute(w, failure)
login.html
{{define "body"}}
<div class="loginfailure">
<h1>{{.Loginfailure}}</h1>
</div>
<div class="signupsuccess">
<h1>{{.Signupsuccess}}</h1>
</div>
<h1>Log In</h1>
<p>Login to access your account</p>
<form action="/login" method="POST">
<div>
<label for="email">Email</label>
<input type="email" name="email" placeholder="Enter your email address" required>
</div>
<div>
<label for="password">Password</label>
<input type="password" name="password" placeholder="Enter your password" required>
</div>
<div>
<input type="submit" value="Login">
</div>
<div>
<a href="/signup" class="link">Signup</a>
</div>
</form>
{{end}}
答案1
得分: 2
始终检查Execute
返回的错误。
在模板中,您不能引用在传递给模板的结构体中不存在的字段,例如当您传递failure
时,{{.Signupsuccess}}
操作会破坏模板,当您传递success
时,{{.Loginfailure}}
会破坏模板。
您可以使用映射,允许引用映射中不存在的键。
success := map[string]string{"Signupsuccess": "您的帐户已成功创建"}
if err := oginTmpl.Execute(w, success); err != nil {
panic(err)
}
failure := map[string]string{"Loginfailure": "请输入正确的电子邮件或密码"}
if err := loginTmpl.Execute(w, failure); err != nil {
panic(err)
}
或者使用一个具有两个字段的单个结构体。
type TemplateData struct {
Signupsuccess string
Loginfailure string
}
success := TemplateData{Signupsuccess: "您的帐户已成功创建"}
if err := oginTmpl.Execute(w, success); err != nil {
panic(err)
}
failure := TemplateData{Loginfailure: "请输入正确的电子邮件或密码"}
if err := loginTmpl.Execute(w, failure); err != nil {
panic(err)
}
英文:
Always check the error returned by Execute
.
Inside the template you can't reference a field that does not exist in the struct that you passed to the template, i.e. when you pass failure
the {{.Signupsuccess}}
action breaks the template, when you pass success
the {{.Loginfailure}}
breaks the template.
You can use maps, referencing map keys that are not present in the map is allowed
success := map[string]string{"Signupsuccess": "Your account is successfully created"}
if err := oginTmpl.Execute(w, success); err != nil {
panic(err)
}
failure := map[string]string{"Loginfailure": "Enter the correct email or password"}
if err := loginTmpl.Execute(w, failure); err != nil {
panic(err)
}
Or use a single struct that has both fields
type TemplateData struct {
Signupsuccess string
Loginfailure string
}
success := TemplateData{Signupsuccess: "Your account is successfully created"}
if err := oginTmpl.Execute(w, success); err != nil {
panic(err)
}
failure := TemplateData{Loginfailure: "Enter the correct email or password"}
if err := loginTmpl.Execute(w, failure); err != nil {
panic(err)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论