YAML 中 nil 和 null 的区别

Difference between nil and null in YAML

我正在使用 Jekyll 制作 Liquid 模板,我有 2 个地方不确定要使用什么:nilnull,或者只是空的。

  1. front matter:

    ---
    layout: nil|null|<empty>
    ---
    
    • 当我使用 nil 时,我收到`构建警告:sitemap.xml 中请求的布局 'nil' 不存在,因为 this thread demonstrates
    • null和blank都没有错误。
  2. 在 Liquid 模板中:

    {% if post.date != nil %}
    
    • nullnil 和空白都有效,但是 "" 在未设置变量时使语句为真。

那我应该用哪个呢?为什么? (这跟Ruby有关系吗?我没加tag。)

对于前面的内容,请使用 null(如您所见)。前面的内容被解释为 YAML,它将空值定义为 nulland possibly also with a tilde ~ 尽管我从未尝试过)。

在液体模板中,大多数情况下最好不要进行比较:

{% if post.date %}

因为这通常会实现您想要做的事情。

在 YAML 中,术语“nil”用于表示值的明确缺失,而“null”用于表示未知或未定义的值。