-
-
Notifications
You must be signed in to change notification settings - Fork 495
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
Add next and previous aliases to collection items #529
Comments
Try this one:
You can read more about pagination on the pagination docs. |
Did you read the full question? The title doesn't tell the whole story. Pretty sure your answer is going to produce the double output I talked about. |
Oh no, I'm so sorry. I'm just reading the title and make comments, maybe sleepy. ✌️ |
Have you tried using the Did you try defining your own collection if keeping the items outside of eleventyConfig.addCollection('outsideOfInputDir', c => {
return c.getFilteredByGlob('some/where/outside/of/input/dir/**/*.md');
}); |
@jevets Yes, I forgot to mention that my attempt to put the files outside my input directory involved trying to build the collection in the configuration, but it came up empty. I think it's because only files in the input directory ever get added to the |
(Assuming you checked out #211.) Yeah, you're right that files only get added to collections if they're in
Gonna have to keep the post markdown files inside of I'm gonna clone your repo later on or setup a separate example to dig more into this. But for now, let me know if any above assumptions are incorrect or if any other info to consider. |
Those assumptions are correct. Right now, my index page just has all the posts since there are not many (page size 100, let's say). I updated the
What does this give me?
There are a couple remaining problems (in my opinion):
|
Here's some code to solve the second remaining problem (post titles in next/prev post links). And a screenshot of it running locally. // blog/index.liquid
{% assign previousIndex = pagination.pageNumber | minus: 1 %}
{% assign previousItem = collections.posts[previousIndex] %}
{% assign nextIndex = pagination.pageNumber | plus: 1 %}
{% assign nextItem = collections.posts[nextIndex] %}
<pre>
{% if previousItem.data.title %}
Previous title: {{ previousItem.data.title }}
Previous URL: {{ pagination.previousPageHref }}
{% endif %}
{% if nextItem.data.title %}
Next title: {{ nextItem.data.title }}
Next URL: {{ pagination.nextPageHref }}
{% endif %}
</pre> |
Regarding the first problem:
Are you referring to the the disqus config? this.page.url = 'https://thejohnfreeman.com{{ page.url }}'; |
A couple notes on the previous/next post titles and links.
|
The first problem is regarding the URL of each post on the home page: <table class="table">
<tbody>
{%- for post in collections.post reversed -%}
<tr>
<td>{{ post.date | utcDate: 'YYYY[ ]MMMM[ ]D' }}</td>
<td><a href="{{ post.url }}">{{ post.data.title }}</a></td>
<!-- this ^ url has to be constructed manually to match what I use in the page template -->
</tr>
{%- endfor -%}
</tbody>
</table> I've thought about trying to add some function or data to link within a collection without using pagination, but I haven't dug into it yet. I think I would prefer that approach considering I'd have to do essentially the same thing in pagination to get the neighboring titles. |
Yeah, if each post markdown file was its own template, its |
@thejohnfreeman I do not know if this would be of any help for you, but here is what I came up with to bring the previous and next links to a portfolio where each photo is a post from a collection: I created 2 shortcodes in my eleventyConfig.addShortcode('previous', (collections, [tag], {inputPath}) => {
// Assumes the first tag to be the filter for the post to be "paginated".
const collec = collections[tag];
for (let i = 0; i <= collec.length; i++) {
if (collec[i+1] && collec[i+1].data.page.inputPath === inputPath) {
return `<a href="${ collec[i].data.page.url }">Previous</a>`;
}
}
});
eleventyConfig.addShortcode('next', (collections, [tag], {inputPath}) => {
// Assumes the first tag to be the filter for the post to be "paginated".
const collec = collections[tag];
for (let i = 1; i <= collec.length-1; i++) {
if (collec[i-1] && collec[i-1].data.page.inputPath === inputPath) {
return `<a href="${ collec[i].data.page.url }">Next</a>`;
}
}
}); These 2 shortcodes are to be used this way in the template or layout: <!-- POST goes here -->
{% previous collections, tags, page %} | {% next collections, tags, page %} |
@morgaan Have tried a variation on this but can’t seem to handle what happens if there is no
...everything comes up blank (except on the very first post, which does indeed show a “Next” but no “Previous” as is desired in this case). In other words, they all fail the test. If I remove the Update: Ah, I see now. The |
@thejohnfreeman @brycewray maybe I don't fully understand what it is you're trying to achieve, but doesn't something like this work? // .eleventy.js
eleventyConfig.addCollection("posts", function(collection) {
const coll = collection.getFilteredByTag("posts");
for(let i = 0; i < coll.length ; i++) {
const prevPost = coll[i-1];
const nextPost = coll[i + 1];
coll[i].data["prevPost"] = prevPost;
coll[i].data["nextPost"] = nextPost;
}
return coll;
}); This way you'll get access to the next and previous post anywhere you're using the collection: {{ prevPost.url }} {{ prevPost.data.title }} |
@pascalw Yes, was just about to sit down and try basically that exact approach (i.e., Update: Works perfectly. You da man! 💯 Thanks very much!! |
Can also confirm the solution by @pascalw works very nicely for me as well. Hats off to you! |
Gonna swap this issue to the enhancement queue to adopt something like @pascalw’s solution into core #529 (comment)
|
Shipping with 0.11.0 https://www.11ty.dev/docs/filters/collection-items/ |
@pascalw you're a lifesaver, somehow the collection-items didn't work (maybe cause I'm using a different tag name?) or I must have done something wrong. Your solution of adding collection into the config files worked! |
It seems like pagination is the only way to get URLs to the "next" and "previous" items in a collection. Then I can create a template with this front-matter:
Here's the rub:
config.dir.input
) then they don't create a collection..html
documents: one for the template itself, and one for its page in the collection.How can I paginate without the extra output documents?
The text was updated successfully, but these errors were encountered: