Get page.url from pagination item? #1767
-
Sorry for the lengthy post, tl;dr at the bottom. I'm using pagination to create pages from data (a quiz). On each page I store some information in JavaScript localStorage (the user answers). On the last page I display an overview of all pages (correct answers compared to answers given by the user). The problem here is that the pages in the overview are sorted by data values instead of the default order. Let me explain: The data structure is like this (frontmatter inside quiz.md):
On the last page I display all survey results sorted by great_worries (simplified example):
My custom filter looks like this in my eleventy.js:
Here is my problem: to identify the localStorage key I need some sort of unique id for each page and each result on the last page. I thought of using page.url and that kind of works, but I can't get the page.url in the for loop on the last page. The example in the Eleventy docs uses pagination.hrefs[ loop.index0 ], but that doesn't work because I sort by values (sortByGreatestWorries) and not by default order. Is there some way to get page.url of the item inside the for-loop? A {{ item | dump }} doesn't show anything like that. Or is there any other way to get a unique id of each page? I might use the title, but that kind of seems a bit error prone. Example: https://github.com/iwm-donath/quiz-test tl;dr: Is there some way to get page.url for each item inside a "for item in pagination.pages" loop that is not in default order or any other way to uniquely identify a page? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
I think you could look into using the I assume you have a collection for quiz pages e.g. all the markdown files (page-one.md.. etc) in a directory
Using
You could hack together a URL and assign that value to a new key in pagination ---js
{
pagination: {
data: "testdata",
size: 2,
before: function(data) {
return data.map(page => {
page['id'] = `some unique identifier`;
}
}
},
testdata: [
{
title: "The Title",
survey_results: {
great_worries: 36.2,
some_worries: 45.2,
no_worries: 17.1
}
},
{
title: "The Title 2",
survey_results : {
great_worries: 47.2,
some_worries: 32.2,
no_worries: 19.1
}
}
]
}
--- which would map to: [
{
title: "The Title",
survey_results: {
great_worries: 36.2,
some_worries: 45.2,
no_worries: 17.1
},
id: some-identifier
},
{
title: "The Title 2",
survey_results: {
great_worries: 47.2,
some_worries: 32.2,
no_worries: 19.1
},
id: some-identifier
}
] This should start you off for one potential solution. If you provide a link to your code or a reproducible example, I'm happy to have another look and provide a more accurate answer. @iwm-donath |
Beta Was this translation helpful? Give feedback.
-
I wrote a longer comment to your reply here. A shorter and more succinct answer is: Inside <main {% if not quiz.answer_page %}data-id="{{ quiz.title }}"{% endif %}> with: <main data-id="{{ page.url }}"> So then each <main data-id="/quiz/this-is-what-the-region-cares-about/1/">
...
<main data-id="/quiz/this-is-what-the-region-cares-about/9/> <!-- Last page -->
This way, without the
Now you have a unique identifier for each page including the last using
This way you can stick with <div class="flex-center worry-progress">
{% for item in pagination.pages %}
{% if not item.answer %}
<div{% if page.url == pagination.href.last %} class="worry-progress--filled"{% endif %}></div>
{% endif %}
{% endfor %}
</div> Since the above code is already wrapped in Hopefully this provides you with a better way of identifying each page and being able to access the last page of pagination data inside a |
Beta Was this translation helpful? Give feedback.
I wrote a longer comment to your reply here. A shorter and more succinct answer is:
Inside
worry.njk
replacewith:
So then each
<main>
element has a uniquedata-id
custom attribute:This way, without the
if
…