Go template and function
Asked Answered
T

2

5

In my go code I often use if like this

if user && user.Registered { }

equivalent code in go templates would be

{{ if and .User .User.Registered }} {{ end }}

Unfortunately code in the template fails, if .User is nil :/

Is it possible to achieve the same thing in go templates?

Troublesome answered 13/2, 2017 at 16:5 Comment(0)
C
12

The template and function does not do short circuit evaluation like the Go && operator.

The arguments to the and function are evaluated before the function is called. The expression .User.Registered is always evaluated, even if .User is nil.

The fix is to use nested if:

 {{if .User}}{{if .User.Registered}}  {{end}}{{end}}

You can avoid the nested if or with by using a template function:

func isRegistered(u *user) bool {
  return u != nil && u.Registered
}

const tmpl = `{{if isRegistered .User}}registered{{else}}not registered{{end}}`

t := template.Must(template.New("").Funcs(template.FuncMap{"isRegistered": isRegistered}).Parse(tmpl))

playground example

Coachwhip answered 13/2, 2017 at 16:8 Comment(2)
I'm trying to escape using nested ifs ..because it would duplicate too much template code in betweenTroublesome
even though this isn't the real solution i'm gonna accept it .Troublesome
D
4

Another option is to use the {{with}} action instead of the and template function.

Quoting from package doc of text/template:

{{with pipeline}} T1 {{end}}
    If the value of the pipeline is empty, no output is generated;
    otherwise, dot is set to the value of the pipeline and T1 is
    executed.

Using {{with}} often results in cleaner and shorter code, as inside the {{with}} the dot . is already set to the non-empty "wrapper", the .User in our case; moreover you don't have to worry about how and if the arguments of the and template function are evaluated.

Your template rewritten:

{{with .User -}}
    {{if .Registered}}REGISTERED{{end}}
{{- end}}

Testing it without and with a user:

t := template.Must(template.New("").Parse(tmpl))

fmt.Println("No user:")
if err := t.Execute(os.Stdout, nil); err != nil {
    panic(err)
}

u := struct{ Registered bool }{true}
fmt.Printf("User: %+v\n", u)
if err := t.Execute(os.Stdout, map[string]interface{}{"User": u}); err != nil {
    panic(err)
}

Output (try it on the Go Playground):

No user:
User: {Registered:true}
REGISTERED
Diecious answered 13/2, 2017 at 16:17 Comment(3)
unfortunately, this is the same as using nested ifs, which i'm trying not to useTroublesome
@Troublesome You did not mention that in your question.Diecious
sorry i thought it was obvious what i wanted to achieve :(Troublesome

© 2022 - 2024 — McMap. All rights reserved.