Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between nil and null in YAML

I'm working on a Liquid template in Jekyll, and I have 2 places where I'm not sure what to use: nil, null, or just empty.

  1. In the front matter:

    ---
    layout: nil|null|<empty>
    ---
    
    • When I use nil, I get `Build Warning: Layout 'nil' requested in sitemap.xml does not exist, as this thread demonstrates.
    • null and blank both have no error.
  2. In Liquid templating:

    {% if post.date != nil %}
    
    • null, nil, and blank all work, but "" makes the statement true, when the variable is not set.

So which should I use? Why? (Does this have anything to do with Ruby? I didn't add the tag.)


1 Answers

For the front matter, use null (as you've already seen). The front matter is interpreted as YAML which defines a null value as null (and possibly also with a tilde ~ though I've never tried that).

In the liquid templates, for the most part you'd be better off with leaving the comparison out:

{% if post.date %}

As that typically achieves what you're trying to do.

like image 84
briantist Avatar answered Dec 04 '25 01:12

briantist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!