Show default content in a template if an object is nil otherwise show based on the set property

Ecognium picture Ecognium · Sep 25, 2015 · Viewed 14.9k times · Source

In my template, I would like to include some default meta tags (90% of the time). However, when a specific property is set, I would like to show a different set of text.

I know I can set an anonymous struct and set a property with either "default" or "some-x". However, this means, I need to add an anonymous struct to 90% of my handlers that just currently pass nil.

Is there way to do something like

{{if eq . nil}} 
   // default meta tag
{{else if eq .MetaValue "some-x"}} 
   //other
{{end}}

If I try something like my above code, it compiles but doesn't do what I want. Appreciate any suggestions on how to handle it properly without adding a lot of boiler plate.

Thanks!

Answer

Cerise Limón picture Cerise Limón · Sep 25, 2015
{{if not .}}
   output when . is nil or otherwise empty including
     false, 0, and any array, slice, map, or string of length zero
{{else if eq .MetaValue "some-x"}}
       // some-x case
{{else}} 
       // other case
{{end}}