尝试在Jekyll中打印发布日期失败,未定义方法`strftime'

jvc*_*c26 4 ruby jekyll

我一直在使用Liquid扩展来重新格式化基于Jekyll的网站上的日期,例如:

<p>{{ post.date | date_to_string }}</p>
Run Code Online (Sandbox Code Playgroud)

这在我的index.html页面中工作正常,它只需要最近的五个帖子,然后通过帖子迭代它们.但是,当我尝试在_layouts/base.html模板中呈现这样的日期时,这会失败.

我试过了:

{{ page.date | date_to_string }}
Run Code Online (Sandbox Code Playgroud)

{{ page.title }}工作没有问题,并{{ page.date}}在我没有液体过滤器使用时渲染,例如,输出2012-03-12 00:00:00 +0000.

为什么date_to_string过滤器会因为提供的输入而失败{{ page.date }}.我收到以下错误:

Liquid Exception: undefined method `strftime' for nil:NilClass in base
Run Code Online (Sandbox Code Playgroud)

构建失败了.谢谢你的帮助!

jos*_*y10 8

在我的网站上我使用

{{ page.date | date: "%d %B %Y" }}
Run Code Online (Sandbox Code Playgroud)

它从markdown文件中获取日期.并且呈现如下:

26 December 2012

有关额外阅读,请参阅这些链接:

Jekyll日期格式化工作如何?

http://liquid.rubyforge.org/classes/Liquid/StandardFilters.html#M000012

编辑:在评论部分回答您的问题:

如果你想使用date_to_string,你必须像这样调用它:

{{ site.time | date_to_string }}
Run Code Online (Sandbox Code Playgroud)

还有{{ site.time | date_to_long_string }一个将用它的完整形式写出月份,例如.十一月不是十一月

资源

https://github.com/mojombo/jekyll/wiki/Liquid-Extensions