如何按日期条件过滤列表?
How to filter a list by date condition?
我正在尝试通过查找结束日期小于或等于今天的所有项目来过滤我的列表。
我知道在 Liquid 中我可以使用 where:
进行过滤以查找符合条件的项目。例如,如果我想获得今天的会议列表,我可以这样做:
{% assign todays_conferences = (site.conferences | where: 'date_end', today) %}
但是,当我尝试获取即将举行的会议时,我无法执行同样的操作:
{% assign upcoming_conferences = (site.conferences | where: 'date_end', ??? | sort: 'date_start') %}
这是因为我不想匹配某个值 one-to-one,而是试图根据日期比较来查找项目。找遍了也没找到方法
如何用这种方式过滤列表?我希望我不必诉诸顺序循环并在每一行上执行 if 语句。
从 Jekyll 3.2 开始,您可以使用 where_exp
而不是使用 where
并使用该表达式为真的对象过滤数组:
{% assign upcoming_conferences = (site.conferences | where_exp: 'date_end', 'date_end < site.time' | sort: 'date_start') %}
您可以将 site.time
替换为任何其他有效日期。
我正在尝试通过查找结束日期小于或等于今天的所有项目来过滤我的列表。
我知道在 Liquid 中我可以使用 where:
进行过滤以查找符合条件的项目。例如,如果我想获得今天的会议列表,我可以这样做:
{% assign todays_conferences = (site.conferences | where: 'date_end', today) %}
但是,当我尝试获取即将举行的会议时,我无法执行同样的操作:
{% assign upcoming_conferences = (site.conferences | where: 'date_end', ??? | sort: 'date_start') %}
这是因为我不想匹配某个值 one-to-one,而是试图根据日期比较来查找项目。找遍了也没找到方法
如何用这种方式过滤列表?我希望我不必诉诸顺序循环并在每一行上执行 if 语句。
从 Jekyll 3.2 开始,您可以使用 where_exp
而不是使用 where
并使用该表达式为真的对象过滤数组:
{% assign upcoming_conferences = (site.conferences | where_exp: 'date_end', 'date_end < site.time' | sort: 'date_start') %}
您可以将 site.time
替换为任何其他有效日期。