0

I'm making a job listing page using Jekyll and I decided to create a separate data file with the job positions available.

I'd like to add a condition where if there's no positions available (the data file is empty), a specific message would appear.

So I have a job.html where the listing should be displayed and the code I tried for showing the listing or message is:

<!-- Jobs -->
<section class="jobs">
    {% if site.data.jobs == '' %}
        <h1>No job openings right now!</h1>
    {% else %}
        {% for job in site.data.jobs %}
            <h1>{{ job.title }}</h1>
            <h2>{{ job.link }}</h2>
        {% endfor %}
    {% endif %}
</section>

It didn't work, so I tried to hack it by only verifying if there's empty title tags, like so:

<!-- Jobs -->
<section class="jobs">
    {% if site.data.jobs.title == '' %}
        <h1>No job openings right now!</h1>
    {% else %}
        {% for job in site.data.jobs %}
            <h1>{{ job.title }}</h1>
            <h2>{{ job.link }}</h2>
        {% endfor %}
    {% endif %}
</section>

Also did not work. Any ideas how to solve this?

marcanuy
  • 23,118
  • 9
  • 64
  • 113

1 Answers1

2

An empty "jobs" data file will make site.data.jobs be false, so you can check that to display the message (for statement won't execute if that is the case):

{% unless site.data.jobs  %}
    <h1>No job openings right now!</h1>
{% endunless%}

If the data file doesn't exists at all, then site.data.jobs will be nil so the above code would work too.

Other solution

Another approach can be to check the size of the site.data.jobs array:

    {% assign jobs_size = site.data.jobs | size %}
    {% if jobs_size == 0 %}
        <h1>No job openings right now!</h1>
    {% else %}
    ...
marcanuy
  • 23,118
  • 9
  • 64
  • 113