-
-
Notifications
You must be signed in to change notification settings - Fork 506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can we offer "Created" and "Last Modified" dates for each template? #869
Comments
According to https://www.11ty.dev/docs/dates/ the items
|
Thanks for the response @brycewray. But, this doesn't work. index.md
site.njk
It outputs following:
|
@mehtapratik True, that's a different matter. Sorry; I misunderstood what you were trying to do. In my site's case, I use a more manual approach (which is probably what you want to avoid). For example, in a post's front matter I might have:
And then, in the appropriate template (omitting some styling items irrelevant to this discussion):
As for
|
Thanks @brycewray. Hello @zachleat: Is this supported in current version or any plans to include it in future releases? |
I'd very much like to see this too. Maybe more future proof than simply adding another pre-processed A basic syntax could look like // custom function
eleventyConfig.addKeyTransform("key-name", function(key) {});
// pre-defined function
eleventyConfig.addKeyTransform("key-name", "date"); |
I want the data @brycewray put in his front matter:
on all of my pages' data automatically. I've been fooling around with global computed data to try to find a way to extract the file modification time by running This is what I tried in const fs = require('fs')
module.exports = {
created: (data) => data.length?fs.statSync(data.inputPath).birthtime:undefined,
modified: (data) => data.length?fs.statSync(data.inputPath).mtime:undefined,
} I have to check Next, in my nunjuck template: <footer>
date: {{eleventyComputed.modified}}
</footer> And the html output is: <footer>date: function () {
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
return obj[val].apply(obj, args);
}
</footer> Which dumps all that weird javascript right out onto the footer. A spectacular failure. Absolute catastrophe. I give up. I'm pretty sure this is possible with computed data, but I haven't figured it out. |
@keith24 I think that's correct. Your eleventyComputed.js file's I recreated your code in a new project and got the following: ---
title: Homepage
permalink: /
---
<footer>
<p>created: {{ eleventyComputed.created }}</p>
<p>created(): {{ eleventyComputed.created() }}</p>
<p>created(page): {{ eleventyComputed.created(page) }}</p>
<hr/>
<p>modified: {{ eleventyComputed.modified }}</p>
<p>modified(): {{ eleventyComputed.modified() }}</p>
<p>modified(page): {{ eleventyComputed.modified(page) }}</p>
</footer> And my output is: <footer>
<p>created: function () {
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
return obj[val].apply(obj, args);
}</p>
<p>created(): </p>
<p>created(page): Tue Jan 26 2021 23:50:44 GMT-0800 (Pacific Standard Time)</p>
<hr/>
<p>modified: function () {
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
return obj[val].apply(obj, args);
}</p>
<p>modified(): </p>
<p>modified(page): Wed Jan 27 2021 00:01:52 GMT-0800 (Pacific Standard Time)</p>
</footer> But I also had to revise the eleventyComputed.js file slightly, since the const fs = require('fs')
module.exports = {
created: (data) => data?.inputPath ? fs.statSync(data.inputPath).birthtime : undefined,
modified: (data) => data?.inputPath ? fs.statSync(data.inputPath).mtime : undefined,
}; TL:DR: I think your code [mostly] works, but you'll need to pass in the current |
If you don't want to pass in the // in your .eleventy.js config file:
eleventyConfig.addShortcode("created", function () {
return this.page?.inputPath ? fs.statSync(this.page.inputPath).birthtime : undefined;
});
eleventyConfig.addShortcode("modified", function () {
return this.page?.inputPath ? fs.statSync(this.page.inputPath).mtime : undefined;
}); Now, in my one random Nunjucks test file, I can add this: <p>created (shortcode): {% created %}</p>
<p>modified (shortcode): {% modified %}</p> OUTPUT<p>created (shortcode): Tue Jan 26 2021 23:50:44 GMT-0800 (Pacific Standard Time)</p>
<p>modified (shortcode): Wed Jan 27 2021 00:21:09 GMT-0800 (Pacific Standard Time)</p> It doesn't look like <p>created (filter): {{ page.inputPath | fileDate }}</p>
<p>created (filter): {{ page.inputPath | fileDate("birthtime") }}</p>
<p>modified (filter): {{ page.inputPath | fileDate("mtime") }}</p> // somewhere in your .eleventy.js config file:
eleventyConfig.addFilter("fileDate", (inputPath, key="birthtime") => {
return inputPath ? fs.statSync(inputPath)[key] : undefined;
}); OUTPUT<p>created (filter): Tue Jan 26 2021 23:50:44 GMT-0800 (Pacific Standard Time)</p>
<p>created (filter): Tue Jan 26 2021 23:50:44 GMT-0800 (Pacific Standard Time)</p>
<p>modified (filter): Wed Jan 27 2021 00:32:07 GMT-0800 (Pacific Standard Time)</p> Last one... an async Nunjucks shortcode, since you mentioned a Nunjucks template: eleventyConfig.addNunjucksAsyncShortcode("fileDateShortcode", async function (label="", key="birthtime") {
const inputPath = this.page?.inputPath;
if (!inputPath) {
return "";
}
const stats = await fs.promises.stat(inputPath);
const date = new Date(stats[key]);
return `${label} ${date?.toLocaleDateString()}`.trim();
}); <p>{% fileDateShortcode "CrEaTeD:" %}</p>
<p>{% fileDateShortcode "MoDiFiEd:", "mtime" %}</p> <p>CrEaTeD: 2021-01-26</p>
<p>MoDiFiEd: 2021-01-27</p> |
Any update on this? I tried @brycewray's idea, but it doesn't seem to work. In the rendered version I get "Wed Apr 14 2021 22:00:00 GMT+0200 (Central European Summer Time)" :( |
I got it to work. Note that only the last example brycewray gave actually converts to a readable string. I think it's done by the |
I am not really a fan of additions like this because of the unreliability of file creation and modified times. See also https://www.11ty.dev/docs/dates/#collections-out-of-order-when-you-run-eleventy-on-your-server I do think a better way forward is #142 But I’ll put this in the enhancement queue and let folks vote on it |
|
I'm not following how #142 helps with the original issue. The question was how can I display both the created and last modified dates without manually editing them. Currently it seems it is an either/or situation for |
Yes, this is something I'm working through right now as well. For the purpose of a blog site I'm working I want to be able to use For the purpose of using the existing setup to having a working sitemap page that updates automatically, I just use |
Just for the record, this is possible to implement today using computed data: https://www.11ty.dev/docs/data-computed/ |
It’s worth also linking to #867 which doesn’t allow multiple dates but does allow further customization of primary date behavior. |
Let's say I want to put two dates on my blog. Date it was originally created and date it was last modified. How can I do this using 11ty?
I thought following could work. But, now I understand it won't because value of
createdDate
will not be transformed.Am I missing something? Is there a better way of handling this scenario?
The text was updated successfully, but these errors were encountered: