如何在构建时在Jekyll页面中插入最后更新的时间戳? [英] How to insert the last updated time-stamp in Jekyll page at build time?
问题描述
我想在Jekyll构建时间为每个帖子自动插入最后更新的时间戳(不是页面的date
变量),如何实现?我想我必须声明一个变量,但是我不确定如何将值赋给该变量.
I would like to automatically insert a last updated timestamp (Not the date
variable for the page) for each post at Jekyll build time, how to achieve that? I think I have to declare a variable but I am not sure how to assign the value to that variable.
例如,有时候我更新一个旧帖子,除了显示发布日期,我还想显示最近的更新日期.
For example, some time I update an old post, beside showing the post date, I also want to show the last update date.
我尝试了{{Time.now}}
,但似乎不起作用.
I have tried {{Time.now}}
but seems does not work.
推荐答案
唯一具有modified_time
的集合是site.static_files
.在我们的案例中不是那么有用.
The only collection that has a modified_time
is site.static_files
. Not so useful in our case.
在Jekyll网站上获取帖子的last-modified-date
的一种方法是使用钩子(文档)
One way to get the last-modified-date
for posts in your Jekyll site is to use a hook (documentation).
_plugins/hook-add-last-modified-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)关于日期Jekill Liquid日期格式设置主题的出色文章.
It's now available in your posts as : {{ page.last-modified-date }}
.
And you can format this date with the a date filter like {{ page.last-modified-date | date: '%B %d, %Y' }}
. See the Alan W. Smith excellent article on date Jekill Liquid date formating topic.
重要通知:挂钩在Github页面上不起作用.
Important notice : hooks are not working on Github pages.
这篇关于如何在构建时在Jekyll页面中插入最后更新的时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!