I was looking at Content Dates and saw the following
Last Modified: automatically resolves to the file鈥檚 last modified date
So my thought was to do the following on my posts:
date: Last Modified
---
Some content
and in my template
<body>
{{content | safe}}
Posted on {{date}}
</body>
However, the generated content will be literally "Posted on Last Modified"
Is this not the intended use of this value? Or am I missing something here?
I am getting a similar result on a single post template page when using date: Last Modifieid.
But it does work when accessing the date via a collection of posts (i.e. in loops over collections.post).
Certainly does appear that Last Modified only casts properly when accessed via collection items. I'll dig a bit more into it.
Not sure if we can consider this a bug... "in a collection" from the docs:
Add a date key to your front matter to override the default date (file creation) and customize how the file is sorted in a collection.
It would be nice if Eleventy did sort it out for use outside of collections, as well.
Let's get @zachleat's input... we'll probably need to create a feature request and get it into the enhancement queue.
I'm surprised nobody's noticed/encountered this, but on second thought I think it adds up:
A publish date is typically unchanged after publishing, while a Last Modified date makes sense within the context of a collection of posts. (maybe a list of "Recently Updated Posts")
Haha yeah, I also found it kinda weird that there was no one else noticing this in past issues because it felt like such an obvious thing to do.
If that's not the intended purpose that's fine, I don't mind making a small PR to make it more obvious in the docs, as given that the date can be used in templates, I feel like it's a mistake that is easy to make.
Yeah, I think it is intended to only apply to collections.
However, it actually does seem to be available under the page variable in your single file template.
Mind giving this a try in your template?
{{ page.date }} instead of {{ date }}
<body>
{{content | safe}}
Posted on {{page.date}}
</body>
This seems to work for me, actually.
Woo! Yes, that works for me too:
Is this a thing we can possibly rely on going forward or an undocumented thing that could change?
I think you're safe to rely on it for the future! @zachleat will probably chime in, if not.
Ah @jevets you鈥檙e a lifesaver, I forgot about that 馃槶
Yes, it will be supported moving forward鈥攖here are tests around this and it is on the official documentation https://www.11ty.io/docs/data/#page-variable-contents%3A
This is an automated message to let you know that a helpful response was posted to your issue and for the health of the repository issue tracker the issue will be closed. This is to help alleviate issues hanging open waiting for a response from the original poster.
If the response works to solve your problem鈥攇reat! But if you鈥檙e still having problems, do not let the issue鈥檚 closing deter you if you have additional questions! Post another comment and I will reopen the issue. Thanks!
@zachleat Is there a way to get the date from the frontmatter AND the last modification date of the page? It seems like it's either or.
I have the same question as @tcurdt -- I want to provide the published date as well as the last modified date, is this possible?
@kahboom I ended up using a lo-fi workaround that's actually even better.
I ended up adding a "updated" field to the frontmatter. Not using the last modification date allows for better control. Fixing a typo does not really warrant to be called an "update" in my book.
@tcurdt The problem is that the date field is parsed to a JavaScript date object, meaning you can transform it in the output using any of the prototype methods on the Date object, while a custom field is simply passed through as a string. Currently AFAIK there is no way to process custom fields as JavaScript date objects.
Most helpful comment
@kahboom I ended up using a lo-fi workaround that's actually even better.
I ended up adding a "updated" field to the frontmatter. Not using the last modification date allows for better control. Fixing a typo does not really warrant to be called an "update" in my book.