Show last modified time?

Hi,

I want to show when a particular page was last modified. For this, I can rely on the mtime of the actual .md file. But how do I output this in jekyll? I guess I am after something like page.lastmod but that does not seem to exist?

There’s a handy plugin for this!

Just need this in your config:

plugins:
  - jekyll-last-modified-at

# Optional. The default date format, used if none is specified in the tag.
last-modified-at:
    date-format: '%d-%b-%y'

And then you can put this in a file where you want the value to be displayed: {% last_modified_at %}.

Thanks.

I am currently not using any plugins and am a bit hesitant on any such external dependencies… Was hoping I could do this natively with jekyll…

Totally fair!

Since plugins are just written in Ruby, if you can recreate the code for a plugin you can use it directly with your site without any external calls by putting the .rb file in your _plugins directory.

Looks like someone did this here: How to set lastmod to file modified date? - #3 by michaelbach

This works. Thanks!

I added the plugin and added this to my template:

{% last_modified {{ page.path }} %}

How do I then format this date?

This does not seem to work:

{% last_modified {{ page.path }} | date: "%Y-%m-%d" %}

Hmm… I got this working on my macbook.

When I then deploy this on my linux server, I get this error:

      Generating...
  Liquid Exception: no implicit conversion of Time into String in jekyll-site/_layouts/recipe.html
/usr/share/rubygems-integration/all/gems/liquid-5.4.0/lib/liquid/tag.rb:51:in `render_to_output_buffer': no implicit conversion of Time into String (TypeError)
	from /usr/share/rubygems-integration/all/gems/liquid-5.4.0/lib/liquid/block_body.rb:80:in `render_node'
	from /usr/share/rubygems-integration/all/gems/liquid-5.4.0/lib/liquid/block_body.rb:230:in `render_node'
	from /usr/share/rubygems-integration/all/gems/liquid-5.4.0/lib/liquid/block_body.rb:213:in `render_to_output_buffer'
	from /usr/share/rubygems-integration/all/gems/liquid-5.4.0/lib/liquid/document.rb:41:in `render_to_output_buffer'
	from /usr/share/rubygems-integration/all/gems/liquid-5.4.0/lib/liquid/template.rb:194:in `render'
	from /usr/share/rubygems-integration/all/gems/liquid-5.4.0/lib/liquid/template.rb:204:in `render!'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/liquid_renderer/file.rb:39:in `block (3 levels) in render!'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/liquid_renderer/file.rb:59:in `measure_counts'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/liquid_renderer/file.rb:38:in `block (2 levels) in render!'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/liquid_renderer/file.rb:63:in `measure_bytes'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/liquid_renderer/file.rb:37:in `block in render!'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/liquid_renderer/file.rb:70:in `measure_time'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/liquid_renderer/file.rb:36:in `render!'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/renderer.rb:129:in `render_liquid'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/renderer.rb:192:in `render_layout'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/renderer.rb:161:in `place_in_layouts'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/renderer.rb:93:in `render_document'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/renderer.rb:63:in `run'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/site.rb:578:in `render_regenerated'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/site.rb:563:in `block (2 levels) in render_docs'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/site.rb:562:in `each'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/site.rb:562:in `block in render_docs'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/site.rb:561:in `each_value'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/site.rb:561:in `render_docs'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/site.rb:210:in `render'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/site.rb:80:in `process'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/command.rb:28:in `process_site'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/commands/build.rb:65:in `build'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/commands/build.rb:36:in `process'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/command.rb:91:in `block in process_with_graceful_fail'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/command.rb:91:in `each'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/command.rb:91:in `process_with_graceful_fail'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/lib/jekyll/commands/build.rb:18:in `block (2 levels) in init_with_program'
	from /usr/share/rubygems-integration/all/gems/mercenary-0.4.0/lib/mercenary/command.rb:221:in `block in execute'
	from /usr/share/rubygems-integration/all/gems/mercenary-0.4.0/lib/mercenary/command.rb:221:in `each'
	from /usr/share/rubygems-integration/all/gems/mercenary-0.4.0/lib/mercenary/command.rb:221:in `execute'
	from /usr/share/rubygems-integration/all/gems/mercenary-0.4.0/lib/mercenary/program.rb:44:in `go'
	from /usr/share/rubygems-integration/all/gems/mercenary-0.4.0/lib/mercenary.rb:21:in `program'
	from /usr/share/rubygems-integration/all/gems/jekyll-4.3.1/exe/jekyll:13:in `<top (required)>'
	from /bin/jekyll:25:in `load'
	from /bin/jekyll:25:in `<main>'

The jekyll version is the same on both.

No errors on MBP but errors on linux.

This is the layout file referenced:

$ cat _layouts/recipe.html
---
layout: default
---
<div class="nav">
  <a href="/">Forsiden</a>
  &nbsp;
  →
  &nbsp;
  <a href="/recipes/">Opskrifter</a>
  &nbsp;
  →
  &nbsp;
  <a href="{{ page.url }}">{{ page.title }}</a>
</div>
<div class="content">
  {{ content }}
</div>
<hr>
<div class="byline">
  Opdateret
  {% last_modified {{ page.path }} %}

</div>
{% if page.tags %}
<div class="tags">
  Tags:
  {% assign tags_sorted = page.tags | sort_natural %}
  {% for tag in tags_sorted %}
  {% if forloop.last == false %}
  <a href="/tags/#tag-{{ tag }}">{{ tag }}</a>,
  {% else %}
  <a href="/tags/#tag-{{ tag }}">{{ tag }}</a>
  {% endif %}
  {% endfor %}
</div>
{% endif %}
{% if page.images %}
<hr>
<h2>Billeder</h2>
<div>
    {% assign images_sorted = page.images | sort_natural %}
    {% for image in images_sorted %}
    <img alt="{{ image }}" src="{{ site.baseurl }}/recipe-images/{{ image }}" class="recipe-image">
      {{ image }}
    {% endfor %}
</div>
{% endif %}

Removing this line makes the error go away on linux:

{% last_modified {{ page.path }} %}

But this is what I want to include… So not sure how to fix

Not sure if page.path should be between {{ }} here.

Well, I have tried doing it like this:

{% last_modified page.path %}

Then it no longer fails. But also, it does not print anything in the rendering. It would just not print any date there…

Also, why would it work on Mac but fail on Linux? When jekyll is the same version?

Not sure, but a lot of users have problems with Jekyll on Mac. If you look at the recent issues for the last month or so, I think I saw it at least twice. It is something related to the path where Jekyll is installed on Mac, so in your case maybe in your Linux you are using the wrong version? Idk.

Are you sure it is working on your Mac? I mean, maybe it was not using a refreshed version because of changes in your _config.yml, so you would need to rerun jekyll serve. Also what is your linux solution? GitHub pages?

Yeah, I have had quite a few problems with jekyll on mac.

However, in this case it works on mac and does not work on linux…

I am sure it works on mac because I have tested it and I actually see the date in the rendered output.

My linux solution is simply jekyll running in the shell on debian linux.