Using ISSET and IF/IF NOT in conjunction to build pages

I am trying to use a combination of ISSET and ELSE to populate some content on one of my site pages. In this scenario, I have products which might have either two, one, or no related subproduct fields. I have an appropriate .md page that has parameters set for each description, and if I just deliver the page with the parameters set in the appropriate spots in the HTML everything renders fine, but that would only work if every product had the same number of subproducts available.

What I’m trying to do is first check via ISSET for the parameters for subproduct two, and if it has a value I render the div with both subproduct one and two. If there is nothing set for that paramater for subproduct two, I use ISSET to check if there is a value for subproduct one, and if so render the div with only subproduct one in it. If neither of those is true, I render a div with some plain text. Unfortunately, only the blank text currently renders even for scenarios where I do have subproduct parameters set. Here is my basic code structure:

{{ if (isset .Params.partner "data_product_name_two") }}
<div>
</div>
<div>
</div>
{{ end }}
		                        	
{{ if (isset .Params "partner.data_product_name_one") }}
<div>
</div>
{{ end }}
		                        	
{{ if not (isset .Params "partner.data_product_name_one") }}
<div></div>
{{ end }}

First time posting and first time using Hugo, so please let me know if there’s something I can do to clarify the problem.

Question for you: when you say “If neither of those is true,” are these front matter fields booleans?

No, they are open text. What I should have said is not ‘if either is true’ but ‘if neither exist’. If there is no value set for those parameters, it would pass on to the next test to determine which div to display.

Playing around with it a bit, it seems to actually work if we remove the ISSET and just assert the parameter (same overall structure as above):

{{ if .Params.partner.data_product_name_two}}
{{ if .Params.partner.data_product_name_one}}
{{ if not .Params.partner.data_product_name_two}}

It feels illogical though, as though I am asking it to assume too much.

Keep in mind you can also use with to rebind the context, cut down on characters, make it more tasteful, etc:

{{ with .Params.partner.data_product_name_two }}<div>{{.}}</div>{{ end }}
{{ with .Params.partner.data_product_name_one }}<div>{{.}}</div>{{ else }}<div>Your code for when there is neither</div>{{ end }}

So if there is a Product Two, it will write to the page, and we can assume (based on the logic you provided) that there would then be a Product One, which would print underneath it and not go through to the else statement. If there is no Product Two, but Product One, it will only print Product One. If there is no Product One, we can also assume there is no Product Two, so only your code will write for the instance where there is neither.

Now, this is according to your logic. If there is an instance where there is a Product Two but not a Product One, this will need to be tweaked…slightly.

2 Likes

Thanks! That worked a treat, except that there is something strange happening that wouldn’t have been apparent from my initial description. In each of the

for the respective products, I was already doing an IF to check for an icon depending on product type. However, it seems like you can’t have an IF nested within a WITH. When I was building the page with the included IF in the
, I’d get:

can’t evaluate field Params in type string

Removing that included IF resolved the error.

Is it not possible to have an IF within a WITH?

*edit … changing your WITHs to IFs did work, rendering the page as you’d have expected and allowed me to use the other IF statements within the

Yes, but you need to understand the difference between the two first: with rebounds context. Therefore context is different with with than with if… You can use an else statement with with but not an else if.