Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If condition on empty string not working #1126

Closed
JulSeb42 opened this issue Aug 14, 2019 · 2 comments
Closed

If condition on empty string not working #1126

JulSeb42 opened this issue Aug 14, 2019 · 2 comments

Comments

@JulSeb42
Copy link

JulSeb42 commented Aug 14, 2019

Hi everyone,

I'm having a bug with Jekyll. I try to display a list, and the items in this list will display depending on if a variable is set or not.

For this I'm using the collections, where in every file the different variables of the component are set. But for some files some variables don't exist, so I'd like them not to display. This is my code at the moment, but it's just not displaying anything.
In the list

{% for icon in site.icons %}
          {% if unicode_ultralight %}
               <li>
                    <h2>{{ icon.title }}</h2>
                    <p>{{ icon.unicode_ultralight }}</p>
               </li>
          {% endif %}
{% endfor %}

In the icon.md

title: user
unicode_ultralight: 123

When I try to write {% if unicode_ultralight != empty %} it displays the list, but if I delete the variable it still displays the icon.

I tried all the solutions here #223 but no luck...

Thanks for your answers!


OS: macOS Mojave
Jekyll Version 3.8.6

@ashmaroli
Copy link
Contributor

The issue is with how you're using the front matter variable. unicode_ultralight is not a valid Liquid variable unless you have previously defined it using the assign tag or capture tag.

The correct syntax to use a front matter key is to send the key to the concerned document i.e. icon.unicode_ultralight. Therefore, your snippet should be:

{% for icon in site.icons %}
  {% if icon.unicode_ultralight %}
    <li>
      <h2>{{ icon.title }}</h2>
      <p>{{ icon.unicode_ultralight }}</p>
    </li>
  {% endif %}
{% endfor %}

@JulSeb42
Copy link
Author

It worked, thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants