Reference global data file in js front matter? #2531
-
|
I have a site with several projects and a global data file 'projects.json' listing all project pages. Now on each page, in the front matter, I want to hand its position (index) in this list to the njk-template. I tried with the following, but it complains that 'projects' is not defined. (index should return 1) My global data file _data/projects.json looks like this: Tangential, but maybe helpful for understanding why i set this up the way I did: I need this index number for the next / previous project navigation. In my project-layout.njk this works like this: Thanks a lot in advance for any help! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
OK, I think you need ---js
{
slug: "../projectA/",
eleventyComputed: {
index(data) {
return data.projects.findIndex(x => x.url === data.slug);
}
}
}
---
index: {{ index }}
<a href="{{ projects[(index-1 + projects.length) % projects.length].url }}">previous project</a>
<a href="{{ projects[(index+1 + projects.length) % projects.length].url }}">next project</a>And the generated www/projects/projectA/index.html looks like this: index: 0
<a href="../projectC/">previous project</a>
<a href="../projectB/">next project</a>Basically, I moved the hardcoded URL from the Next, I created a module.exports = {
eleventyComputed: {
index(data) {
return data.projects.findIndex(x => x.url === data.slug);
}
}
};It's the same code, but now in an external file which will be shared by all templates in the ./src/projects/* directory. ---
slug: "../projectB/"
---
index: {{ index }}
<a href="{{ projects[(index-1 + projects.length) % projects.length].url }}">previous project</a>
<a href="{{ projects[(index+1 + projects.length) % projects.length].url }}">next project</a>Where www/projects/projectB/index.html looks like this: index: 1
<a href="../projectA/">previous project</a>
<a href="../projectC/">next project</a>… and www/projects/projectC/index.html looks like this: index: 2
<a href="../projectB/">previous project</a>
<a href="../projectA/">next project</a>Not sure if that is exactly what you were trying to do. I'm not convinced about my logic. Or if you could use collections and the current page URL You could also look at https://www.11ty.dev/docs/filters/collection-items/ and see if that might work for you. Possibly cleaner, although you would lose the ability to use the |
Beta Was this translation helpful? Give feedback.
-
|
wow. just wow. |
Beta Was this translation helpful? Give feedback.
OK, I think you need
eleventyComputed. Here's my modified ./src/projects/projectA.njk:---js { slug: "../projectA/", eleventyComputed: { index(data) { return data.projects.findIndex(x => x.url === data.slug); } } } --- index: {{ index }} <a href="{{ projects[(index-1 + projects.length) % projects.length].url }}">previous project</a> <a href="{{ projects[(index+1 + projects.length) % projects.length].url }}">next project</a>And the generated www/projects/projectA/index.html looks like this:
Basically, I moved the hardcoded URL from the
.findIndex()method into aslugfront…