How to Disable an HTML Element if Data is a Certain Value?

So let’s say I want to make an HTML tag disabled if {{.offer.Status}} == “Ended”. How would I do so?

Use eq to check for equality:

{{ if eq .offer.Status "Ended"  }}{{ end }}

To add to what @digitalcraftsman wrote, can you expand on which template (ie, list/single/home) you are trying to put this in? Just making sure that when you wrote “Data” in the title that you’re talking about a data file. The answer above is correct, but if you want to iterate over pages according to a front matter variable, this is another example:

{{range where .Site.Pages "Params.offer.Status" "!=" "Ended"}}
<!--whatever you want in here...-->

If you are looking to create a list of, say, product names and you had a series of toml files in data/offers for each of the offers, here is another example of defining the included markup negatively (ie, only write those products that aren’t “ended” according to your data file:

   {{range .Site.Data.products}}
         {{if ne .status "Ended"}}

Although I get the feeling the above could be DRYer with use of where perhaps, @digitalcraftsman?

You could get really clever if you’re creating these files individually and want to set expiration dates at creation time by leveraging the .Unix method. Then automate your builds each day. I guess it depends on how many offers you are managing. Hope this helps.

The question should state if the HTML element should be disabled for single pages or if a list should be generated. My answer would cover the check for a single page.

@rdwatters, you made a good addition with your interpretation of the question.

Both examples will do the job but I don’t now which is faster. A benchmark would be interesting. But the I agree that the first example with the usage of where looks more idiomatic since the template func where is all about filtering.

I found the answer. Inside of a tag for example lets say input I did. input {{if eq .offer.Status “Ended”}}disabled{{end}} /input.

Ah, gotcha. You literally wanted to write the word “disabled.” Glad you figured it out.