HUGO

How to check for 'not in' in if statements?

There are several operators for where, but are there also similar operators for if statements? I couldn’t find them in the docs.

For example, I want to execute code when the post’s categories is not “Meta”. The where operators, like not in, won’t work for this:

{{ if isset .Params "categories" }}
  {{ if not in .Params.categories "Meta" }}
      <!-- execute code here -->
  {{ end }}
{{ end }}

I have not tested this, but if takes one boolean as parameter, so the above needs to be written as:

 {{ if (not in .Params.categories "Meta") }}
1 Like

Thanks Bep, for leading me to the right path. :slight_smile: I moved the first parenthesis a one step to the right and it worked. The code is now:

{{ if isset .Params "categories" }}
    {{ if not (in .Params.categories "Meta") }}
        <!-- Hugo code here -->
    {{ end }}
{{ end }}
2 Likes

I can’t get this working. Here is what I have:

{{ range .Site.Pages}}
    {{ if not (in .Params.categories "Nutrition") }}
        <!-- HTML goes here -->
    {{ end }}
{{ end }}

And here is the Hugo error:

ERROR 2018/09/20 10:30:00 Error while rendering “home” in “”: template: index.html:61:7: executing “index.html” at <partial “aside.html”…>: error calling partial: template: partials/aside.html:14:62: executing “partials/aside.html” at <delimit .Params.cate…>: error calling delimit: can’t iterate over

Can anyone spot what I’m doing wrong?

Not sure if a copy/paste error, but you’re missing two of these: {{ end }}

Should look like this:

{{ range .Site.Pages}}
    {{ if not (in .Params.categories "Nutrition") }}
        <! -- HTML goes here -->
    {{ end }}
{{ end }}

Thanks, edited the original post. I was trying to be brief. But, I still can’t get the if not in logic to work.

Post a link to your git repo. Would need to see your project structure to troubleshoot further

The repo is private at the moment. Can I add you temporarily?

That’s fine

Better yet, mark your repo as public temporarily so that others can see as well

What’s your GitHub user ID?

zwbetz-gh

Thanks, added.

Just checking in the my latest codes…

K, done

I tried to workaround with an else clause, but that seems to break as well. For example:

{{ range .Site.Pages}}
    {{ if in .Params.categories "Nutrition" }}
    {{ else }}
        <!-- HTML OUTPUT -->
    {{ end }}
{{ end }}

Results in this error:

ERROR 2018/09/20 14:45:58 Error while rendering "taxonomy" in "": template: taxonomy/category.html:31:7: executing "taxonomy/category.html" at <partial "aside.html"...>: error calling partial: template: partials/aside.html:32:63: executing "partials/aside.html" at <delimit .Params.cate...>: error calling delimit: can't iterate over <nil>

Ignore, the error is related to another function in side the HTML OUTPUT… The else thing works.

Giving a brief summary of the problem and the solution, so that others may benefit if they run into something similar.

@jnthnclrk has an aside element on each of his pages. In this aside there is a list of “Other Categories”. If the current page is a list of posts of category x, then the “Other Categories” list should not show any posts of category x. Additionally, only 4 posts should show in the “Other Categories” section.

Solution (if you know a better way to accomplish this, please do share):

{{ $.Scratch.Set "counter" 0 }}
{{ range .Site.Pages }}
    {{ if not (in .Params.categories $.Title) }}
    {{ $.Scratch.Set "counter" (add ($.Scratch.Get "counter") 1) }}
        {{ if lt ($.Scratch.Get "counter") 5 }}
        <! -- HTML -->
        {{ end }}
    {{ end }}
{{ end }}
1 Like

Works great! I was completely wrong earlier about “if not (in” not working. It works great. I got in a pickle with scoping. Will post better examples in future.

@zwbetz I like your solution and am more than happy with Scratch loop logic, but I wonder if it’s possible to combine the “if not in” category logic with the range line? Because then we can presumably use Hugo’s first 4 function.