Golang模板无法正常工作
我的模板中有一个if-else块。当Golang模板无法正常工作,go,templating,Go,Templating,我的模板中有一个if-else块。当else if为真时,它总是呈现为空,就像else或else if不存在一样 这是我的模板 在本例中,它不呈现任何内容 我还使用了text/template,因为html/template会将页面完全清空 //the template <script> {{if.PassChange}} swal("{{.Lang.Success}}", "{{.Lang.Ple
else if
为真时,它总是呈现为空,就像else
或else if
不存在一样
这是我的模板
在本例中,它不呈现任何内容
我还使用了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})
})
//模板
{{if.PassChange}
swal(“{.Lang.Success}}”,“成功”)
{{end}
{{if.UserExists}
swal(“{.Lang.Fail}}”,“{{.Lang.AlreadyMember}}”,“error”)
{{end}
//渲染部分
BasePath.Get(“/”,func(w http.ResponseWriter,r*http.Request){
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})
图像,尤其是第二个图像,很难阅读。请把课文粘贴到问题中。还有,你用什么库来渲染模板?@MellowMarmot刚刚更新了银河系投反对票的聪明人你能和我们分享一下你难以置信的知识吗?为什么你认为这是一个愚蠢的问题