Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何在Jekyll中按日期对主页上的帖子进行分组?_Ruby_Jekyll_Liquid - Fatal编程技术网

Ruby 如何在Jekyll中按日期对主页上的帖子进行分组?

Ruby 如何在Jekyll中按日期对主页上的帖子进行分组?,ruby,jekyll,liquid,Ruby,Jekyll,Liquid,在Jekyll中,我希望我的主页列出按日期分组的最新帖子,如下所示: 2013年9月6日 职位1 职位2 职位3 2013年9月5日 职位1 职位2 基本上,我只想在循环中的帖子来自与之前处理的帖子不同的日期时,吐出一个日期标题。我试图通过测试for循环中的下一篇文章是否与上一篇文章的日期匹配来实现这一点,并且仅当它不匹配时才显示日期标题。这就是我的液体模板的外观: --- layout: default title: Home Page --- {% assign thedate =

在Jekyll中,我希望我的主页列出按日期分组的最新帖子,如下所示:

2013年9月6日

  • 职位1
  • 职位2
  • 职位3
2013年9月5日

  • 职位1
  • 职位2
基本上,我只想在循环中的帖子来自与之前处理的帖子不同的日期时,吐出一个日期标题。我试图通过测试for循环中的下一篇文章是否与上一篇文章的日期匹配来实现这一点,并且仅当它不匹配时才显示日期标题。这就是我的液体模板的外观:

---
layout: default
title: Home Page
---

{% assign thedate = '' %}

{% for post in site.posts %}

    {% if thedate != post.date | date: "%m-%d-%Y" %}
        <h2>{{ post.date | date: "%A, %B %e, %Y" }}</h2>
    {% endif %}

    {% assign thedate = post.date | date: "%m-%d-%Y" %}

    <h3 class="headline"><a href="{{ post.url }}">{{ post.title }}</a></h3>
    {{ post.content }}
    <hr>

{% endfor %}
---
布局:默认值
标题:主页
---
{%assign thedate=''%}
{site.posts%中的post为%s}
{%if thedate!=post.date |日期:“%m-%d-%Y”}
{{post.date | date:“%A,%B%e,%Y”}
{%endif%}
{%assign thedate=post.date |日期:“%m-%d-%Y”}
{{post.content}}

{%endfor%}
如果不使用
post.date | date:“%m-%d-%Y”
我会简单地说
post.date
它有效,并且帖子被分组在一起,但前提是帖子的日期和时间完全相同(而不仅仅是当月的同一天)。这就是为什么我添加了更具体的
post.date | date:“%m-%d-%Y”


有什么想法吗?非常感谢我们的帮助

通过在此处修改归档解决方案找到了答案:

以下是有效的代码:

layout: default
title: Home Page
---

{% for post in site.posts %}

    {% capture day %}{{ post.date | date: '%m%d%Y' }}{% endcapture %}
    {% capture nday %}{{ post.next.date | date: '%m%d%Y' }}{% endcapture %}

    {% if day != nday %}
        <h5 class="date">{{ post.date | date: "%A, %B %e, %Y" }}</h5>
    {% endif %}
    {{ post.content }}
    <hr>

{% endfor %}
布局:默认设置
标题:主页
---
{site.posts%中的post为%s}
{%capture day%}{{post.date}日期:'%m%d%Y'}{%endcapture%}
{%capture-nday%}{{post.next.date}日期:'%m%d%Y'}{%endcapture%}
{%if-day!=nday%}
{{post.date | date:“%A,%B%e,%Y”}
{%endif%}
{{post.content}}

{%endfor%}
替代解决方案:

以希望在结尾显示的格式直接捕获日期。
(此处:
%A,%B%d,%Y
-->
2012年4月30日,星期一

那么您就不需要经常使用
| date:

{% for post in site.posts %}
  {% capture currentdate %}{{post.date | date: "%A, %B %d, %Y"}}{% endcapture %}
  {% if currentdate != thedate %}
    <h2>{{ currentdate }}</h2>
    {% capture thedate %}{{currentdate}}{% endcapture %} 
  {% endif %}
    <h3><a href="{{ post.url }}">{{ post.title }}</a></h3>
{% endfor %}
{%for site.posts%}
{%capture currentdate%}{{post.date | date:“%A,%B%d,%Y”}{%endcapture%}
{%if currentdate!=日期%}
{{currentdate}}
{%capture thedate%}{{currentdate}{%endcapture%}
{%endif%}
{%endfor%}
生成的HTML:

<h2>Monday, April 30, 2012</h2>
<h3><a href="/2012/04/30/foo/">Foo</a></h3>
<h2>Friday, March 09, 2012</h2>
<h3><a href="/2012/03/09/bar/">Bar</a></h3>
<h3><a href="/2012/03/09/baz/">Baz</a></h3>
2012年4月30日,星期一
2012年3月9日,星期五

这些以前的解决方案是克服以前版本的Jekyll缺点的绝佳且优雅的方法,但幸运的是,在2016年末,过滤器可以更干净地完成这项工作

{% assign postsByDay = 
site.posts | group_by_exp:"post", "post.date | date: '%A, %B %e, %Y'" %}

{% for day in postsByDay %}
  <h1>{{ day.name }}</h1>
    <ul>
      {% for post in day.items %}
        <li><a href="{{ post.url }}">{{ post.title }}</a></li>
      {% endfor %}
    </ul>
{% endfor %}
{%assign postsByDay=
site.posts | group|u by_exp:“post”,“post.date | date:“%A,%B%e,%Y'%”
{postsByDay%中的天的%s}
{{day.name}
    {%用于日内发布。项目%}
  • {%endfor%}
{%endfor%}
文档可在上找到