Conditional for loop on index

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

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

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

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

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

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:

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

The output is:

Name: Azimut

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

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