# Conditional for loop on index

**URL:** <https://talk.jekyllrb.com/t/conditional-for-loop-on-index/5922>\
**Category:** Help\
**Created:** [April 22, 2021, 12:11pm UTC](https://talk.jekyllrb.com/t/conditional-for-loop-on-index/5922 "2021-04-22T12:11:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjplack](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@jjplack](https://talk.jekyllrb.com/u/jjplack)\
**Post date:** [April 22, 2021, 12:11pm UTC](https://talk.jekyllrb.com/t/conditional-for-loop-on-index/5922/1 "2021-04-22T12:11:02Z")

</div>

Hello, someone can inform if is possoble to make a conditional loop that depend if the attribute is not nil?

for exemple:

```
{% for boat in site.boats %}

{{ boat.name }}

{% endfor %}

boat.md
name: Azimut
sell: yes

```

if the sell is not nil, show the boats

---

<div class="post-metadata">

**Author:** ![chuckhoupt](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/chuckhoupt/32/523_2.png) [@chuckhoupt](https://talk.jekyllrb.com/u/chuckhoupt)\
**Post date:** [April 22, 2021, 12:45pm UTC](https://talk.jekyllrb.com/t/conditional-for-loop-on-index/5922/2 "2021-04-22T12:45:17Z")

</div>

An if statement should work for boolean, nil, and undefined values:

```auto
{% if boat.sell %}
{{ boat.name }}
{% endif %}

```

---

<div class="post-metadata">

**Author:** ![jjplack](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@jjplack](https://talk.jekyllrb.com/u/jjplack)\
**Post date:** [April 22, 2021, 1:06pm UTC](https://talk.jekyllrb.com/t/conditional-for-loop-on-index/5922/3 "2021-04-22T13:06:09Z")

</div>

@chuckhoupt thank you very much that’s it worked smoothly 😀

---

<div class="post-metadata">

**Author:** ![BillRaymond](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/billraymond/32/5963_2.png) [@BillRaymond](https://talk.jekyllrb.com/u/BillRaymond)\
**Post date:** [April 22, 2021, 7:32pm UTC](https://talk.jekyllrb.com/t/conditional-for-loop-on-index/5922/4 "2021-04-22T19:32:33Z")

</div>

One more option is to first `assign` the variable and use a `where` clause. This is super useful because you don’t need to do a `for` loop with another `if` clause inside it.

For example, I created a `./_data/boats.yml` file (the ‘.’ represents the top-level of the Jekyll site’s folder structure):

```auto
items:
    - title: boat.md
      name: Azimut
      sell: "Yes"
    - title: boat2.md
      name: Azimut2
      sell:

```

**Note:** The “Yes” is best to be in quotes due to what I think is a problem with Jekyll’s `where` filter when dealing with `truthy` data.

And here is the simplified code:

````auto
{%- assign boats = site.data.boats.items | where: "sell", "Yes" -%}
{%- for boat in boats -%}
    <p>Name: {{ boat.name }}</p>
{%- endfor -%}```

````

The output is:

```auto
Name: Azimut

```

---

<div class="post-metadata">

**Author:** ![chuckhoupt](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/chuckhoupt/32/523_2.png) [@chuckhoupt](https://talk.jekyllrb.com/u/chuckhoupt)\
**Post date:** [April 22, 2021, 8:53pm UTC](https://talk.jekyllrb.com/t/conditional-for-loop-on-index/5922/5 "2021-04-22T20:53:42Z")

</div>

> [@BillRaymond](#):
>
> **Note:** The “Yes” is best to be in quotes due to what I think is a problem with Jekyll’s `where` filter when dealing with `truthy` data.

YAML is tricky because `yes`/`no` and `true`/`false` are booleans, but `"Yes"`, `yea`, `nope`, etc. are strings, which are always truthy.

If one uses “true” booleans for the `sell` field, then the `where_exp` filter can be used to filter like this (the test is equivalent to `{% if boat.sell %}`):

```auto
{% assign boats = site.data.boats.items | where_exp: "boat", "boat.sell" %}

```
