为了账号安全,请及时绑定邮箱和手机立即绑定

如何在同一文件中运行两个匿名结构?

如何在同一文件中运行两个匿名结构?

Go
繁华开满天机 2022-09-19 21:12:40
在此代码中,我有两条消息。1-第一个是注册您的帐户已创建。现在登录到一个帐户,然后2-第二个是登录失败,即如果电子邮件或密码不匹配,则给出错误消息。我已经在文件中调用了这两条消息,但是当我运行此代码时,它更喜欢只运行第一条消息。login.html <div class="loginfailure">        <h1>{{.Loginfailure}}</h1>    </div>它不允许第二个消息,如果调用第二条消息,它将提供空白页。处理程序.go注册成功success := struct{ Signupsuccess string }{Signupsuccess: "Your account is successfully created"}loginTmpl.Execute(w, success)登录失败failure := struct{ Loginfailure string }{Loginfailure: "Enter the correct email or password"}loginTmpl.Execute(w, failure)登录.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 回答

?
喵喵时光机

TA贡献1846条经验 获得超7个赞

始终检查 返回的错误。Execute


在模板内部,您不能引用在传递给模板的结构中不存在的字段,即当您传递操作时会破坏模板,当您传递中断模板时。failure{{.Signupsuccess}}success{{.Loginfailure}}


您可以使用地图,允许引用地图中不存在的地图键


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)

}

或者使用具有两个字段的单个结构


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)

}


查看完整回答
反对 回复 2022-09-19
  • 1 回答
  • 0 关注
  • 76 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号