Golang templating does not work properly

349 views Asked by At

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 in this case, it renders nothing enter image description here

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

There are 1 answers

0
Cerise Limón On BEST ANSWER

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})