Skip to content
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

Unable to override the content date in eleventyComputed property #2199

Closed
mark-reason opened this issue Feb 1, 2022 · 5 comments
Closed

Comments

@mark-reason
Copy link

mark-reason commented Feb 1, 2022

If I set the date like this in eleventy computed it doesn't override the file date:

---js
{
  layout: "layouts/base.html",
  pagination: {
    data: "pages",
    alias: "pg",
    size: 1
  },
  permalink: "/",
  eleventyComputed: {
    date: () => "2022-01-25"
  }
}
---

It does work however when I move this out of a computed property:

---js
{
  layout: "layouts/base.html",
  pagination: {
    data: "pages",
    alias: "pg",
    size: 1
  },
  permalink: "/",
  date: "2022-01-25",
}
---

The reason I need this is I eventually want to use the page data to set the date.

@pdehaan
Copy link
Contributor

pdehaan commented Feb 1, 2022

See also:


I don't like it [at all] but this hacky trick might work:

---js
{
  pagination: {
    data: "pages",
    size: 1,
    alias: "pg",
    addAllPagesToCollection: true
  },
  permalink: "/page/{{ pg.date }}/{{ pg.title | slug }}/",
  eleventyComputed: {
    date(data) {
      const _date = new Date(data.pg.date);
      // HACK: Inject the computed date into the internal `page` object. Eeww.
      data.page.date = _date;
      return _date;
    }
  }
}
---

title={{ pg.title }}
pg.date={{ pg.date }}
eleventyComputed.date={{ date }}
page.date={{ page.date }}

page={{ page | inspect | safe }}

Where my ./src/_data/pages.js looks like this:

module.exports = [
  { title: "page 1", date: "01/02/2022" },
  { title: "page 2", date: "02/03/2022" },
  { title: "page 3", date: "03/04/2022" },
];

OUTPUT (./www/page/03/04/2022/page-3/index.html)

title=page 3
pg.date=03/04/2022
eleventyComputed.date=Fri Mar 04 2022 00:00:00 GMT-0800 (Pacific Standard Time)
page.date=Fri Mar 04 2022 00:00:00 GMT-0800 (Pacific Standard Time)

page={
  date: 2022-03-04T08:00:00.000Z,
  inputPath: './src/index.njk',
  fileSlug: '',
  filePathStem: '/index',
  outputFileExtension: 'html',
  url: '/page/03/04/2022/page-3/',
  outputPath: 'www/page/03/04/2022/page-3/index.html'
}

@mark-reason
Copy link
Author

Thanks @pdehaan!

@TigersWay
Copy link
Contributor

@pdehaan Sorry to add on a closed issue, but is there another way than your - yes hacky 😄 - but very practical solution? It seems the only way to pass "information" from a data set to collection built with pagination.

@pdehaan
Copy link
Contributor

pdehaan commented May 2, 2022

@TigersWay I think my solution above is super hacky, but mostly only because date is a special key in 11ty and we needed to use it to preserve file creation date.

In most cases I think using eleventyComputed or "the before() callback" is probably the best way to go.

@RReverser
Copy link

@pdehaan Thanks a lot for this hack, couldn't come up with any solution so I'm happy I stumbled upon this thread. I used it for both page.date and page.url fields so my blog can now reference posts published on other (corporate) blogs and show them in a list like regular posts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants