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

**URL:** <https://discourse.gohugo.io/t/how-to-check-for-not-in-in-if-statements/1497>\
**Category:** support\
**Created:** [July 30, 2015, 10:46am UTC](https://discourse.gohugo.io/t/how-to-check-for-not-in-in-if-statements/1497 "2015-07-30T10:46:27Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jura](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jura/32/13581_2.png) [@Jura](https://discourse.gohugo.io/u/Jura)\
**Post date:** [July 30, 2015, 10:46am UTC](https://discourse.gohugo.io/t/how-to-check-for-not-in-in-if-statements/1497/1 "2015-07-30T10:46:27Z")

</div>

There are several [operators](http://gohugo.io/templates/functions/) 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 }}

```

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [July 30, 2015, 11:30am UTC](https://discourse.gohugo.io/t/how-to-check-for-not-in-in-if-statements/1497/2 "2015-07-30T11:30:03Z")

</div>

> [@Jura](#):
>
> {{ if not in .Params.categories “Meta” }}

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

```auto
 {{ if (not in .Params.categories "Meta") }}

```

---

<div class="post-metadata">

**Author:** ![Jura](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jura/32/13581_2.png) [@Jura](https://discourse.gohugo.io/u/Jura)\
**Post date:** [July 30, 2015, 12:17pm UTC](https://discourse.gohugo.io/t/how-to-check-for-not-in-in-if-statements/1497/3 "2015-07-30T12:17:02Z")

</div>

> [@bep](#):
>
> 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”) }}

Thanks Bep, for leading me to the right path. 🙂 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 }}

```

---

<div class="post-metadata">

**Author:** ![jnthnclrk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jnthnclrk/32/6430_2.png) [@jnthnclrk](https://discourse.gohugo.io/u/jnthnclrk)\
**Post date:** [September 20, 2018, 2:31pm UTC](https://discourse.gohugo.io/t/how-to-check-for-not-in-in-if-statements/1497/4 "2018-09-20T14:31:45Z")

</div>

> [@Jura](#):
>
> {{ if isset .Params “categories” }} {{ if not (in .Params.categories “Meta”) }} \<!-- Hugo code here –\> {{ end }} {{ end }}

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?

---

<div class="post-metadata">

**Author:** ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)\
**Post date:** [September 20, 2018, 4:10pm UTC](https://discourse.gohugo.io/t/how-to-check-for-not-in-in-if-statements/1497/5 "2018-09-20T16:10:32Z")

</div>

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

Should look like this:

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

```

---

<div class="post-metadata">

**Author:** ![jnthnclrk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jnthnclrk/32/6430_2.png) [@jnthnclrk](https://discourse.gohugo.io/u/jnthnclrk)\
**Post date:** [September 20, 2018, 5:07pm UTC](https://discourse.gohugo.io/t/how-to-check-for-not-in-in-if-statements/1497/6 "2018-09-20T17:07:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)\
**Post date:** [September 20, 2018, 5:23pm UTC](https://discourse.gohugo.io/t/how-to-check-for-not-in-in-if-statements/1497/7 "2018-09-20T17:23:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jnthnclrk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jnthnclrk/32/6430_2.png) [@jnthnclrk](https://discourse.gohugo.io/u/jnthnclrk)\
**Post date:** [September 20, 2018, 5:26pm UTC](https://discourse.gohugo.io/t/how-to-check-for-not-in-in-if-statements/1497/8 "2018-09-20T17:26:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)\
**Post date:** [September 20, 2018, 5:26pm UTC](https://discourse.gohugo.io/t/how-to-check-for-not-in-in-if-statements/1497/9 "2018-09-20T17:26:39Z")

</div>

That’s fine

---

<div class="post-metadata">

**Author:** ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)\
**Post date:** [September 20, 2018, 5:28pm UTC](https://discourse.gohugo.io/t/how-to-check-for-not-in-in-if-statements/1497/10 "2018-09-20T17:28:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jnthnclrk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jnthnclrk/32/6430_2.png) [@jnthnclrk](https://discourse.gohugo.io/u/jnthnclrk)\
**Post date:** [September 20, 2018, 5:29pm UTC](https://discourse.gohugo.io/t/how-to-check-for-not-in-in-if-statements/1497/11 "2018-09-20T17:29:30Z")

</div>

What’s your GitHub user ID?

---

<div class="post-metadata">

**Author:** ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)\
**Post date:** [September 20, 2018, 5:31pm UTC](https://discourse.gohugo.io/t/how-to-check-for-not-in-in-if-statements/1497/12 "2018-09-20T17:31:29Z")

</div>

zwbetz-gh

---

<div class="post-metadata">

**Author:** ![jnthnclrk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jnthnclrk/32/6430_2.png) [@jnthnclrk](https://discourse.gohugo.io/u/jnthnclrk)\
**Post date:** [September 20, 2018, 5:31pm UTC](https://discourse.gohugo.io/t/how-to-check-for-not-in-in-if-statements/1497/13 "2018-09-20T17:31:49Z")

</div>

Thanks, added.

---

<div class="post-metadata">

**Author:** ![jnthnclrk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jnthnclrk/32/6430_2.png) [@jnthnclrk](https://discourse.gohugo.io/u/jnthnclrk)\
**Post date:** [September 20, 2018, 5:32pm UTC](https://discourse.gohugo.io/t/how-to-check-for-not-in-in-if-statements/1497/14 "2018-09-20T17:32:27Z")

</div>

Just checking in the my latest codes…

---

<div class="post-metadata">

**Author:** ![jnthnclrk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jnthnclrk/32/6430_2.png) [@jnthnclrk](https://discourse.gohugo.io/u/jnthnclrk)\
**Post date:** [September 20, 2018, 5:35pm UTC](https://discourse.gohugo.io/t/how-to-check-for-not-in-in-if-statements/1497/15 "2018-09-20T17:35:49Z")

</div>

K, done

---

<div class="post-metadata">

**Author:** ![jnthnclrk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jnthnclrk/32/6430_2.png) [@jnthnclrk](https://discourse.gohugo.io/u/jnthnclrk)\
**Post date:** [September 20, 2018, 6:46pm UTC](https://discourse.gohugo.io/t/how-to-check-for-not-in-in-if-statements/1497/16 "2018-09-20T18:46:10Z")

</div>

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>
```

---

<div class="post-metadata">

**Author:** ![jnthnclrk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jnthnclrk/32/6430_2.png) [@jnthnclrk](https://discourse.gohugo.io/u/jnthnclrk)\
**Post date:** [September 20, 2018, 6:49pm UTC](https://discourse.gohugo.io/t/how-to-check-for-not-in-in-if-statements/1497/17 "2018-09-20T18:49:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)\
**Post date:** [September 21, 2018, 12:51am UTC](https://discourse.gohugo.io/t/how-to-check-for-not-in-in-if-statements/1497/18 "2018-09-21T00:51:35Z")

</div>

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):

```auto
{{ $.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 }}

```

---

<div class="post-metadata">

**Author:** ![jnthnclrk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jnthnclrk/32/6430_2.png) [@jnthnclrk](https://discourse.gohugo.io/u/jnthnclrk)\
**Post date:** [September 21, 2018, 12:41pm UTC](https://discourse.gohugo.io/t/how-to-check-for-not-in-in-if-statements/1497/19 "2018-09-21T12:41:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jnthnclrk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jnthnclrk/32/6430_2.png) [@jnthnclrk](https://discourse.gohugo.io/u/jnthnclrk)\
**Post date:** [September 21, 2018, 1:57pm UTC](https://discourse.gohugo.io/t/how-to-check-for-not-in-in-if-statements/1497/20 "2018-09-21T13:57:14Z")

</div>

@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.

[Next page](https://discourse.gohugo.io/t/how-to-check-for-not-in-in-if-statements/1497.md?page=2)
