If....else with Tags or Fields in string templates

In the documentation, there is this example of if else.

.message('{{ .ID }} is {{ if eq .Level "OK" }}alive{{ else }}dead{{ end }}: {{ index .Fields "emitted" | printf "%0.3f" }} points/10s.')

Makes sense. But if i’m trying to do {{ if eq .tagname “somevalue” }} it doesn’t work.
I cant find any documentation regarding “if … else” with tags or fields in string templates.

Is this “if … else” possible?

Thanks

I figure out some of my issues.
this works:

var message = ‘{{ if eq .Tags.tagname “something” }} writeoutthistext {{ else }} writeoutsomeothertext {{ end }}’.

Now my question is can you string “if” statements together or “elseif”.
I have 4 possible situations, and need to string 4 if statements together.

Where is documentation on this and can this be done?

Thanks

Hello,

You can definitely string them together with an else if. My example is using the .Level tag but I think you might be able to change that to use your tag values, although to be honest I’ve not tried.

{{if eq .Level "CRITICAL"}}
                             some stuff
<!--END CRITICAL ALERT HTML-->
                           

 <!--Begin WARNING Alert-->

{{ else if eq .Level "WARNING"}}
                           some other stuff
<!-- End WARNING Alert Message-->

<!--OK ALERT HTML-->
{{ else}}
                      more stuff      

{{end}}

You should be able to insert as many “else if” as you want. As i say though, not sure if it works with tag values like you want. If your message is working with tag values like that i would assume it should work but ya never know

1 Like