英文:
Golang templating does not work properly
问题
我在我的模板中有一个if else块。当else if为真时,它总是被渲染为空,就好像else或else if不存在一样。
这是我的模板:
<script>
    {{if.PassChange}}
        swal("{{.Lang.Success}}", "{{.Lang.PleaseLogin}}", "success")
    {{end}}
    {{if.UserExists}}
        swal("{{.Lang.Fail}}", "{{.Lang.AlreadyMember}}", "error")
    {{end}}
</script>
在这种情况下,它不会渲染任何内容。
另外,我正在使用text/template,因为html/template会完全发送空页面。
渲染部分的代码如下:
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})
})
希望对你有帮助!
英文:
I have an if else block in my template. when else if is true it is rendered always empty as if else or else if is not there

here is my template

in this case, it renders nothing

And also I am using text/template because html/template  send the page completely empty
//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})
    })
答案1
得分: 2
如果您打印执行模板时的错误信息,您会发现模板无法评估字段PassChange。一个可能的修复方法是向结构体中添加一个PassChange字段。
tpl.ExecResponse(w, struct{PassChange bool; Lang map[string]string ; UserExists bool}{Lang:lang.GetLang(r),UserExists:true})
英文:
If you print the error from executing the template, you will find that the template cannot evaluate the field PassChange.  One possible fix is to add a PassChange field to the struct.
tpl.ExecResponse(w, struct{PassChange bool; Lang map[string]string ; UserExists bool}{Lang:lang.GetLang(r),UserExists:true})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论