Search code examples
jekyllliquid

How do I show a Jekyll Collection item on a specific date? (Liquid syntax issue?)


I guess the most basic question I have is why does using a number at the beginning of a Liquid if statement seem not to work.

This works:

{% if quip.show == "foo" %}

This does not:

{% if quip.show == "1" %}

…but of course there's a lot more to it, here's what I have going on.

I have a Jekyll Collection of little quips I want to show when the site builds on a specific date. A daily status type thing. I setup a collection with the files, made my template, things are marvelous. The issue is making it so only one shows up at a time on the assigned date.

My first thought was to add date: 2020-06-17 to the front matter then run a check to see if that matches today's date but that seems to be conflicting with the Jekyll implied date. So I changed the front matter tag to show: 2020-06-17 just to troubleshoot.

So now I have a for loop that shows all of my quips like this:

{% for quip in site.quips %}
    (this is where my HTML is and it loops and renders each collection file perfectly)
    {% if quip.show == "2020-06-17" %}
        It's working. (this is where the quip HTML will go but I'm isolating it)
    {% endif %}
 {% endfor %}

That's all manual, the front matter literally says show: 2020-06-17 but obviously I'd like it to automatically get the date, more on that in a bit. …but it doesn't print "It's working.". Oddly, when I change the front matter to show: foo in one of my collection files it does show "It's working". Also, when I change it to just the number 1 it doesn't work. So it seems like Jekyll doesn't like numbers as the first character in a ``==`.

So basically this is what I would like to do:

I have a variable set using this:

{% capture my_var %}{{ "now" | date: "%Y-%m-%d"}}{% endcapture %}

I added this at the top of my page to grab the date (after some other things didn't seem to work)

…this outputs cleanly to 2020-06-17 (as confirmed by calling the variable and looking at the HTML output.)

So then I'd like to put this in my collection loop:

{% if quip.show == "{{ my_var }}" %}
    It's Working
{% endif %}

(the quotes around {{ my var }} are tripping me up a bit because the compiler complains if they aren't there, but things don't reliably work without them. No clue there, not the main issue but one that I need to solve).

I mean, the idea is to wrap all of my quip template with this date detector if statement instead of printing "It's working" but that would be a nice proof of concept.

I'm really not opposed to using Jekyll's built in date frontmatter or even the filename -- there's only one per day. I've tried so many things and for some reason I just can't get this to work when I'm using a numeric value in the == statement.

Any help would be much appreciated.


Solution

  • This is happening because the front matter is YAML, and YAML might be more clever than you thought it would.

    date: 2020-06-17
    

    Is an actual valid date for YAML.

    So if I JSON print such a date defined in a front matter, then, it would show as a date:

    {{ quip.date | json }}
    

    2020-06-17 00:00:00 +0200

    So if you want to compare dates from the front matter and from the day, you'll have to format both dates.

    {% assign now = "now" | date: "%Y-%m-%d" %}
    {% assign show = quip.show | date: "%Y-%m-%d" %}
    {% if show == now %}
      <p class="fresh-from-the-day">quip.title</p>
    {% endif %}