multiple conditions in if statement Go templates

Berry Jones picture Berry Jones · Mar 26, 2017 · Viewed 27.4k times · Source

how can I have multiple conditions in an if statement inside a template?

I tried this code:

{{ if .condition1 && .condition2 }}
    <!-- SHOW SOMETHING -->
{{ end }}

But it doesn't work. (in fact it panics)

Answer

shizhz picture shizhz · Mar 26, 2017

You need to use function and, like:

{{ if and .condition1 .condition2 }}
<!-- SHOW SOMETHING -->
{{ end }}

Here's an working example: https://play.golang.org/p/g_itE5ggCM