gpt4 book ai didi

Golang 模板无法正常工作

转载 作者:数据小太阳 更新时间:2023-10-29 03:39:12 26 4
gpt4 key购买 nike

我的模板中有一个 if else block 。当 else if 为真时,它始终呈现为空,就好像 elseelse if 不存在一样

这是我的模板

in this case在这种情况下,它什么也不渲染 enter image description here

而且我正在使用 text/template 因为 html/template 发送的页面完全是空的

//the template
<script>
{{if.PassChange}}
swal("{{.Lang.Success}}", "{{.Lang.PleaseLogin}}", "success")
{{end}}
{{if.UserExists}}
swal("{{.Lang.Fail}}", "{{.Lang.AlreadyMember}}", "error")
{{end}}
</script>




//rendering part
BasePath.Get("/", func(w http.ResponseWriter, r *http.Request) {
tpl.ExecResponse(w, struct{Lang map[string]string ; UserExists bool}{Lang:lang.GetLang(r),UserExists:true})
})

最佳答案

如果打印执行模板的错误,您会发现模板无法评估字段PassChange。一种可能的解决方法是向结构中添加一个 PassChange 字段。

tpl.ExecResponse(w, struct{PassChange bool; Lang map[string]string ; UserExists bool}{Lang:lang.GetLang(r),UserExists:true})

关于Golang 模板无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41255403/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com