液体

2011-08-17 77 views
12

我使用的是液体,杰基尔张贴在我的乐队的网站(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语句我已经尝试了一些,但我想不出如何将发布日期比较当前日期。

任何人都可以帮忙吗?

回答

25

基于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... 

捕获的数字可能为字符串,而不是数字行动,可以将类型转换回使用下面的技巧编号:

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

有没有什么办法来过滤帖子,只包含循环以外的未来事件?随着事件数量的增加,我现在的成像测试会比'event.date'要昂贵。他们提到使用'with_scope' [这里](https://groups.google.com/forum/#!topic/locomotivecms/4-3gZbv03V0),但Jekyll不认为它是一个标签。 – Roy

+0

这非常有帮助,谢谢! – aaandre

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年2月19日):
CloudCannon现在具有计划构建,您可以简单地指定每天构建一次您的项目。如果您使用CloudCannon,我推荐用户[here]的答案。

+1

请不要jquery。 –

+0

将此重写为非jQuery非常简单。 – JoostS

+1

我不打算学习一个10年陈旧的技术,我也不打算猜测。在目前的形式下,这个答案对我来说是无用的,但如果你不使用jQuery来重写它,我会删除我的downvote。 –