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

Fix pagination and eleventy computed for .njk engine #3120

Merged
merged 9 commits into from
Jun 10, 2024

Conversation

alifeee
Copy link
Contributor

@alifeee alifeee commented Dec 10, 2023

closes #2512, #2837, #3013

Introduction

Hi, I hope you are well.

There is a problem where if you use a Nunjucks and a template file like

---
layout: base
pagination:
  data: books
  size: 1
  alias: book
permalink: /{{ book.shortname }}/
eleventyComputed:
  title: "{{ book.name }}"
---
{{ title }}

All eleventyComputed variables are blank. See #2512, #2837, #3013 for discussion.

This problem was introduced by c6158b0, a commit between 2.0.0-canary.8 and 2.0.0-canary.9.

New test

I have added a test which fails:

https://github.com/alifeee/eleventy/blob/301d2c09d76a6bc717a0a8ceda37a1bdb7cdbb42/test/PaginationTest.js#L833-L863

here is a simplification (in the actual test, it tests all templating languages)

// ./_data/books.json
[
    {
        "id": 1,
        "name": "The Effervescent adventures of Paul Mescal",
        "shortname": "paul-mescal"
    }
]
{{! ./_includes/base.njk }}
---
title: Books
---
<title>{{ title }}</title>
{{! ./book.njk }}
---
layout: base
pagination:
  data: books
  size: 1
  alias: book
eleventyComputed:
  title: "{{ book.name }}"
---
// PaginationTest.js
test("Pagination and eleventyComputed data, issues #2512, #2837, #3013", async (t) => {
  let elev = new Eleventy(`./test/stubs-3013/njk/`, `./test/stubs-3013/njk/_site`, {
      source: "cli",
      runMode: "build",
    });
    await elev.init();
    let written = await elev.toJSON();

    t.is(written[0].content, "<title>The Effervescent adventures of Paul Mescal</title>");
});

Result

This test failed

image

The fix

I spent some time commiting c6158b0 bit-by-bit and the fix i discovered was this:

- // set(target, prop, value) {
- //  return Reflect.set(target, prop, value);
- // }
+ set(target, prop, value) {
+   return Reflect.set(target, prop, value);
+  }

I am not fully sure why this is the fix, but I think it is to do with being able to set the data.eleventyComputed on a Proxy object.

If you re-add this removed code from c6158b0, then, compiling a template complains as the object cannot be assigned unless set exists as a property above. Thus, this is the fix I implemented with alifeee@301d2c0

And now the test passes

Thank you for your time. I hope this is the correct branch to target with this change.

alifeee added a commit to lipu-tenpo/liputenpo.org that referenced this pull request Dec 14, 2023
tailwind is not intuitive, and adds complexity
nunjucks does not work properly -> 11ty/eleventy#3120
@wgeorgecook
Copy link

Would love to see this merged as this specific issue plagued me all day.

@zachleat zachleat added this to the Eleventy 3.0.0 milestone Jun 10, 2024
@zachleat
Copy link
Member

Shipping with 3.0.0-alpha.11. Thank you!

@zachleat
Copy link
Member

Accidentally merged this in 2.x. Again, this will ship with 3.0.0-alpha.11.

Thank you!

@alifeee
Copy link
Contributor Author

alifeee commented Jun 11, 2024

cheers ;) enjoy

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

Successfully merging this pull request may close these issues.

None yet

3 participants