Golang模板不能正常工作

时间:2016-12-21 05:00:31

标签: go templating

我的模板中有一个if else块。当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})
    })

1 个答案:

答案 0 :(得分:3)

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

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