如何在构建时在Jekyll页面中插入最后更新的时间戳?

时间:2016-04-21 01:37:04

标签: jekyll

我想在Jekyll构建时为每个帖子自动插入最后更新的时间戳(不是页面的date变量),如何实现?我想我必须声明一个变量,但我不确定如何将值赋给该变量。

例如,有些时候我更新旧帖子,除了显示发布日期之外,我还想显示上次更新日期。

我尝试了{{Time.now}},但似乎无效。

1 个答案:

答案 0 :(得分:16)

唯一拥有modified_time的集合是site.static_files。在我们的案例中没那么有用。

获取Jekyll网站帖子的last-modified-date的一种方法是使用钩子(documentation)。

<强> _plugins /钩附加上次修改-date.rb

Jekyll::Hooks.register :posts, :pre_render do |post|

  # get the current post last modified time
  modification_time = File.mtime( post.path )

  # inject modification_time in post's datas.
  post.data['last-modified-date'] = modification_time

end

现在可以在帖子中找到:{{ page.last-modified-date }}。 您可以使用{{ page.last-modified-date | date: '%B %d, %Y' }}等日期过滤器格式化此日期。请参阅Alan W. Smith excellent article on date Jekill Liquid date formating topic

重要提示:挂钩无法在Github页面上运行。