【问题标题】:Trying to print post date in Jekyll fails with undefined method `strftime'尝试在 Jekyll 中打印发布日期失败,未定义方法“strftime”
【发布时间】:2012-12-18 07:16:00
【问题描述】:

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

<p>{{ post.date | date_to_string }}</p>

这在我的index.html 页面中运行良好,该页面只获取五个最近的帖子,然后逐个帖子地迭代它们。但是,当我尝试在我的 _layouts/base.html 模板中呈现这样的日期时,这会失败。

我试过了:

{{ page.date | date_to_string }}

{{ 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

构建失败。感谢您的帮助!

【问题讨论】:

标签: ruby jekyll


【解决方案1】:

在我的网站上我使用

{{ page.date | date: "%d %B %Y" }}

它从 Markdown 文件中获取日期。并且是这样渲染的:

26 December 2012

查看这些链接以获得更多阅读内容:

How does Jekyll date formatting work?

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

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

如果你想使用 date_to_string 你必须这样称呼它:

{{ site.time | date_to_string }}

还有{{ site.time | date_to_long_string } 会以完整的形式写出月份,例如。 11 月不是 11 月。

来源

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

【讨论】:

  • 根据您链接的第二个答案,date_to_string 是 Jekyll 添加到液体的标准过滤器的一部分。它适用于 GH Pages,这可能意味着它与 Ruby/Jekyll/Liquid 版本混淆有关,只是想知道是否有人对此有意见?
  • 为什么要转成字符串?为什么不使用我在网站上的方式生成它?
  • 本质上,它与| date "%d %b %Y" 完全相同,我只是对 Jekyll 和 Ruby 的某些组合似乎不起作用这一事实感兴趣,这似乎很奇怪!
猜你喜欢
  • 2023-01-20
  • 2018-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-19
  • 2015-02-27
  • 2015-04-07
相关资源
最近更新 更多