Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert the last updated time-stamp in Jekyll page at build time?

Tags:

jekyll

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.

I have tried {{Time.now}} but seems does not work.

like image 388
X.Creates Avatar asked Apr 21 '16 01:04

X.Creates


1 Answers

The only collection that has a modified_time is site.static_files. Not so useful in our case.

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

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.

Important notice : hooks are not working on Github pages.

like image 152
David Jacquel Avatar answered Sep 19 '22 18:09

David Jacquel