【问题标题】:Comparing dates in liquid比较液体中的日期
【发布时间】:2011-10-28 13:53:45
【问题描述】:

我正在使用 Liquid 和 Jekyll 在我的乐队网站 (http://longislandsound.com.au) 上发布日期

我想要的是自动隐藏旧日期,这样我就不必再进去删除它们了。我认为最好的方法是将发布日期与当前日期进行比较,并且仅在日期在未来时才显示帖子,但我不知道如何做到这一点。

这是当前代码:

<ul id="dates">
{% for post in site.posts reversed %}
<a href="{{post.link}}">
<li>
    <div class="date">
        <span class="day">{{post.date | date: "%d"}}</span><br />
        <span class="month">{{post.date | date: "%b"}}</span>
        <span class="week">{{post.date | date: "%a"}}</span>
    </div>
    <div class="details">
        <span class="venue">{{post.venue}}</span><br />
        <span class="town">{{post.town}}</span>
    </div>
</li>
</a>
{% endfor %}
</ul>

我尝试了一些 if 语句,但我不知道如何将发布日期与当前日期进行比较。

谁能帮忙?

【问题讨论】:

    标签: templates date liquid jekyll


    【解决方案1】:

    基于date-math-manipulation-in-liquid-template-filterget-todays-date-in-jekyll-with-liquid-markup,您应该可以使用{{'now'}}{{site.time}} 和难以找到的unix 时间戳日期过滤器| date: '%s' 的组合

    {% capture nowunix %}{{'now' | date: '%s'}}{% endcapture %}
    {% capture posttime %}{{post.date | date: '%s'}}{% endcapture %}
    {% if posttime < nowunix %}
    ...show posts...
    

    捕获的数字可以用作字符串,而不是数字,并且可以使用以下 hack 类型转换回数字:

    {% assign nowunix = nowunix | plus: 0 %}
    

    【讨论】:

    • 有没有办法过滤帖子以仅包含循环之外的未来事件?随着事件数量的增加,我对nowevent.date 的成像测试会变得昂贵。他们提到使用with_scopehere,但 Jekyll 并没有将其识别为标签。
    【解决方案2】:

    虽然这段代码有效:

    {% capture nowunix %}{{'now' | date: '%s'}}{% endcapture %}
    {% capture posttime %}{{post.date | date: '%s'}}{% endcapture %}
    {% if posttime < nowunix %} ...show posts...
    

    它只在构建期间执行。如果您希望您的网站真正自动更新,您应该让 javascript 进行隐藏。

    从这种液体开始:

    {% for item in site.events %} 
      <div future-date="{{ item.date | date: '%Y%m%d' }}">...</div> 
    {% endfor %}
    

    并添加这个javascript:

    function getCompareDate() { 
      var d = new Date(), 
          month = '' + (d.getMonth() + 1), 
          day = '' + d.getDate(), 
          year = d.getFullYear(); 
      if (month.length < 2) month = '0' + month; 
      if (day.length < 2) day = '0' + day; 
      return [year, month, day].join(''); 
    } 
    
    $('[future-date]').each(function() { 
      if($(this).attr('future-date') < getCompareDate()) $(this).hide(); 
    });
    

    解决方案在这里找到:http://jekyllcodex.org/without-plugin/future-dates/


    更新(2018-02-19):
    CloudCannon 现在已经安排了构建,您可以简单地指定每天构建一次项目。如果你使用CloudCannon,我推荐用户[here]的回答。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 2012-03-15
    • 2021-10-24
    • 1970-01-01
    • 2020-07-08
    相关资源
    最近更新 更多