Skip to content

Double-layered pagination? #332

Closed
Closed

Description

@zellwk

I'm creating a blog. Each post are tagged. I've created each tag with the zero maintenance tag pages tip.

But each tag can potentially contain lots of posts. Can Eleventy create paginated tag pages?

EG:

  1. /tags/tag1/
  2. /tags/tag1/page2
  3. /tags/tag1/page3
  4. /tags/tag2/
  5. /tags/tag2/page2
  6. /tags/tag3/

And so on.

Activity

  1. changed the title [-]Double-layed pagination?[/-] [+]Double-layered pagination?[/+] on Dec 6, 2018
  2. jevets commented on Dec 6, 2018

    @jevets

    I don't think there's a straightforward way. There are a couple issues about this but I don't think a solution was found, without some custom modules/preprocessing.
    #294
    #308

    _includes files can't use pagination in their own frontmatter.

    But there's this idea from the docs (I haven't tried this approach for anything):
    https://www.11ty.io/docs/permalinks/#ignore-the-output-directory

    Basically, let Eleventy compile a file and output it to the _includes directory.
    Then your other file will include the compiled file.

    edit: This statement, specifically:

    Writes to _includes/index.html even though the output directory is _site. This is useful for writing child templates to the _includes directory for re-use in your other templates.

    If you give this a go and if it works, please let us know how it goes.

  3. edwardhorsford commented on Dec 6, 2018

    @edwardhorsford
    Contributor

    Another idea might be to add a custom collection that outputs the data you want. Essentially recreate all the tag page collections, but one entry per page, rather than one per tag.

  4. zachleat commented on Dec 7, 2018

    @zachleat
    Member

    This is a fascinating idea—I’ve been mulling it over since you posted it.

    I think you can do this, but you would have to flatten your custom collection to a single layer to do it.

    Use custom collections: https://www.11ty.io/docs/collections/#advanced%3A-custom-filtering-and-sorting.

    Here’s how it would work:

    // note that this uses the lodash.chunk method, so you’ll have to require that
    eleventyConfig.addCollection("doublePagination", function(collection) {
    	// Get unique list of tags
    	let tagSet = new Set();
    	collection.getAllSorted().map(function(item) {
    		if( "tags" in item.data ) {
    			let tags = item.data.tags;
    
    			// optionally filter things out before you iterate over?
    			for (let tag of tags) {
    				tagSet.add(tag);
    			}
    
    		}
    	});
    
    	// Get each item that matches the tag
    	let paginationSize = 3;
    	let tagMap = [];
    	let tagArray = [...tagSet];
    	for( let tagName of tagArray) {
    		let tagItems = collection.getFilteredByTag(tagName);
    		let pagedItems = lodashChunk(tagItems, paginationSize);
    		// console.log( tagName, tagItems.length, pagedItems.length );
    		for( let pageNumber = 0, max = pagedItems.length; pageNumber < max; pageNumber++) {
    			tagMap.push({
    				tagName: tagName,
    				pageNumber: pageNumber,
    				pageData: pagedItems[pageNumber]
    			});
    		}
    	}
    
    	/* return data looks like:
    		[{
    			tagName: "tag1",
    			pageNumber: 0
    			pageData: [] // array of items
    		},{
    			tagName: "tag1",
    			pageNumber: 1
    			pageData: [] // array of items
    		},{
    			tagName: "tag1",
    			pageNumber: 2
    			pageData: [] // array of items
    		},{
    			tagName: "tag2",
    			pageNumber: 0
    			pageData: [] // array of items
    		}]
    	 */
    	//console.log( tagMap );
    	return tagMap;
    });
    

    and then in your template it might look like this:

    ---
    pagination:
      data: collections.doublePagination
      size: 1
      alias: tag
    permalink: /tags/{{ tag.tagName }}/{% if tag.pageNumber %}{{ tag.pageNumber + 1 }}/{% endif %}
    ---
    
    {% for post in tag.pageData %}
      Iterate over the items.
    {% endfor %}
    

    Does that make sense?

  5. zachleat commented on Dec 7, 2018

    @zachleat
    Member

    Seems to work!

    image

  6. edwardhorsford commented on Dec 7, 2018

    @edwardhorsford
    Contributor

    This is really cool!

  7. zellwk commented on Dec 9, 2018

    @zellwk
    Author

    Looks great! Lemme test it out as soon as I can :D

  8. removed
    waiting-to-closeIssue that is probably resolved, waiting on OP confirmation.
    on Jan 8, 2019
  9. zachleat commented on Jan 8, 2019

    @zachleat
    Member

    This is an automated message to let you know that a helpful response was posted to your issue and for the health of the repository issue tracker the issue will be closed. This is to help alleviate issues hanging open waiting for a response from the original poster.

    If the response works to solve your problem—great! But if you’re still having problems, do not let the issue’s closing deter you if you have additional questions! Post another comment and I will reopen the issue. Thanks!

  10. hidegh commented on Feb 19, 2021

    @hidegh

    The vredeburg theme uses very similar approach! You can check it out there!

  11. TheReyzer commented on Sep 6, 2022

    @TheReyzer

    Hi, I'm using your code @zachleat to paginate my tags, but it takes all collections and I just need to paginate my collection "store" that have some tags like "Adventure", "RPG"... I already see the filter API but I don't know how to use that. Can someone help me, please?

  12. pdehaan commented on Sep 6, 2022

    @pdehaan
    Contributor

    @TheReyzer I think the secret would be to maybe
    replace collection.getAllSorted() with something like collection.getFilteredByTag("store").

    eleventyConfig.addCollection("doublePagination", function(collection) {
    	// Get unique list of tags
    	let tagSet = new Set();
    -   collection.getAllSorted().map(function(item) {
    +   collection.getFilteredByTag("store").map(function(item) {
  13. TheReyzer commented on Sep 6, 2022

    @TheReyzer

    I've tried but it keeps paging between categories, ideally, it would be paging between items in each category, not the categories themselves. anyway thanks, but I'm giving up on 11ty unfortunately I can't continue.

  14. TheReyzer commented on Sep 15, 2022

    @TheReyzer

    I finally make it work right for my case. But I need make it reverse, I use | reverse filter, and just reverse the post on page not the array, like this:

    With | reverse:
    page 1:
      3
      2
      1
    page 2:
      6
      5
      4
    
    But I need this result:
    page 1:
      6
      5
      4
    page 2:
      3
      2
      1
    

    The code:

    module.exports = function (eleventyConfig) {
       eleventyConfig.addPassthroughCopy("images");
       eleventyConfig.addPassthroughCopy("admin");
       eleventyConfig.addPassthroughCopy('css');
    
       var lodashChunk = require('lodash.chunk');
    
       // note that this uses the lodash.chunk method, so you’ll have to require that
       eleventyConfig.addCollection("doublePagination", function(collection) {
          // Get unique list of tags
          //let tagSet = new Set();
          var tagSet = new Set(collection.getAllSorted().flatMap((post) => post.data.tags || []));
           
          // Get each item that matches the tag
          let paginationSize = 3;
          let tagMap = [];
          let tagArray = [...tagSet];
          for( let tagName of tagArray) {
             let tagItems = collection.getFilteredByTag(tagName);
             let pagedItems = lodashChunk(tagItems, paginationSize);
              //console.log( tagName, tagItems.length, pagedItems.length );
             for( let pageNumber = 0, max = pagedItems.length; pageNumber < max; pageNumber++) {
                
                tagMap.push({
                   tagName: tagName,
                   pageNumber: pageNumber,
                   pageSize: pagedItems.length,
                   pageData: pagedItems[pageNumber]
                });
             }
          }
          /* return data looks like:
             [{
                tagName: "tag1",
                pageNumber: 0
                pageData: [] // array of items
             },{
                tagName: "tag1",
                pageNumber: 1
                pageData: [] // array of items
             },{
                tagName: "tag1",
                pageNumber: 2
                pageData: [] // array of items
             },{
                tagName: "tag2",
                pageNumber: 0
                pageData: [] // array of items
             }]
          */
          //console.log(tagMap);
          return tagMap;
       });
    }
    

    Can someone help me, please?

  15. pdehaan commented on Sep 15, 2022

    @pdehaan
    Contributor

    @TheReyzer Does something like let pagedItems = lodashChunk(tagItems.reverse(), paginationSize); work?

  16. TheReyzer commented on Sep 15, 2022

    @TheReyzer

    @TheReyzer Does something like let pagedItems = lodashChunk(tagItems.reverse(), paginationSize); work?

    Absolutely, thank you so much! Help a lot.

  17. m4rrc0 commented on Mar 26, 2023

    @m4rrc0

    For reference: I feel like the following comment might be useful to some people landing here.

    Originally posted by @therealshark in #136 (comment)

    BlogTemplate.js

    class BlogTemplate {
      language;
      data() {
        return {
          pagination: {
            data: `posts-${this.language}`,
            size: 1
          },
          permalink: props => {
            return `/${this.language}/blog${
              props.pagination.pageNumber === 0
                ? ''
                : `/${props.pagination.pageNumber + 1}`
            }/index.html`;
          }
        };
      }
      render(props) {
        // final template...
      }
    }
    
    export default BlogTemplate;
    

    BlogTemplateEn.11ty.js

    import BlogTemplate from './BlogTemplate';
    class BlogTemplateEn extends BlogTemplate {
      language = 'EN';
    }
    

    That way you don't need to duplicate code.

  18. m4rrc0 commented on Mar 26, 2023

    @m4rrc0

    I am relatively new to Eleventy, so maybe I am being naïve in the way I approach this problem but...
    Why can't we 'just' return arrays from JS templates @zachleat ?

    I see 2 options:

    1. Being able to return arrays from the data and the render methods.
      a. If we return arrays from both, they must have the same size. Indexes are used to associate the data to the rendered output.
      b. Alternatively, we can return an array from one method and an object from the other. In this case, the object is associated with each array entry (for example, having one render template for many different data objects)
    2. Being able to export an array of classes.

    In any case, we still make the build fail if writing to the same output twice so it is up to the us to avoid colliding premalinks.

  19. adliymeri commented on Aug 27, 2023

    @adliymeri

    Does @zachleat code work for any key in the front matter, let's say "category", or is it only for "tags" key?

  20. j9t commented on Dec 10, 2023

    @j9t
    Contributor

    Also just in case anyone is still following this, it seems that this doesn’t play well with filterTagList—i.e., tags previously ignored would now end up with paginated pages in the output.

    Hacked around to make it work but kept running run into issues, like tags filtered being ignored in the output altogether, which is the other extreme. Need to go of this unsolved for the time being, but if this happens to ring a bell to anyone, any feedback appreciated!


    Edit: Not the most elegant solution (it would be nicer to hook this up to “filterTagList”), but it turned out to be fairly easy to do by adding something like tagSet.delete("TAGTOBEEXCLUDED");.

  21. zachleat commented on Dec 22, 2023

    @zachleat
    Member

    I noted the above example used the collections API but here’s another way to do it in Eleventy 3.0 with the pagination before callback: https://github.com/zachleat-cc/demo-cloudcannon-i18n/blob/d986f5943cd3daa29ba2391624dfeb5f032eee66/src/songs.liquid#L8-L37

    Editing to include the front matter code sample here:

    ---js
    // <script>
    const pagination = {
    	data: "collections.songs",
    	alias: "song",
    	size: 1,
    	addAllPagesToCollections: true,
    	before: function(paginationData, {languages, streamingServiceNames}) {
    		let ret = [];
    		let songIndex = 0;
    		for(let song of paginationData) {
    			for(let lang of languages) {
    				let pageData = {
    					eleventyPaginationGroupNumber: songIndex,
    					language: lang,
    					slug: lang !== "en" ? `/${lang}/` : "/",
    					urls: {},
    				};
    
    				for(let key in song.data) {
    					if(key === lang) {
    						Object.assign(pageData, song.data[key])
    					} else if(key === "urls") {
    						for(let serviceKey in song.data.urls) {
    							pageData.urls[streamingServiceNames[serviceKey]] = song.data.urls[serviceKey];
    						}
    					}
    				}
    
    				ret.push(pageData);
    			}
    			songIndex++;
    		}
    
    		return ret;
    	}
    }
    
    const layout = "layout.liquid";
    
    const tags = "songPages";
    
    const permalink = "{{ song.slug }}{{ song.title | slugify }}/";
    
    const eleventyComputed = {
    	lang: "{{ song.language }}",
    	metadata: {
    		title: "Taylor Swift: {{ song.title }}",
    		titleHtml: "<a href='{{ song.slug }}'>Taylor Swift</a> <span>{{ song.title }}</span>",
    		cta: {
    			url: "{{ song.slug }}",
    			text: "{{ 'songs' | i18n }}",
    		}
    	}
    };
    ---
  22. TheReyzer commented on Feb 27, 2024

    @TheReyzer

    This looks like a better way to do it, great Zach!

  23. j9t commented on Oct 11, 2025

    @j9t
    Contributor

    Here’s how it would work:

    I’ve been using this solution for some time but eventually ran into massive performance issues, where this double pagination cost nearly 40 seconds of build time (on a 10,000s files Eleventy site but a 2024 M4 Mac).

    To serve anyone running into performance issues, sharing an iteration I vibe-engineered using Junie, bringing this down to <1 second. Likely needs some tweaking for other projects, will also not need all details (like date fallbacks).

    // Create (date-sorted) collection for tag pagination
    eleventyConfig.addCollection('paginationDouble', function (collectionApi) {
      // Initialize (required) variables
      const paginationSize = 250;
      const tagMap = [];
      // Build tag → items map in a single pass and cache publication timestamps once
      const tagToItems = new Map();
      const items = collectionApi.getAllSorted();
    
      for (const item of items) {
        // Cache a valid publication date and its timestamp once
        let d = item.data.date_publication;
        if (!(d instanceof Date) || isNaN(d.getTime())) {
          d = new Date(1970, 0, 1); // Jan 1, 1970 fallback
          item.data.date_publication = d;
        }
        if (item.data.__pubTs == null) {
          item.data.__pubTs = d.getTime();
        }
        // Distribute item across its tags (excluding “posts”)
        const tags = item.data.tags || [];
        for (const tag of tags) {
          if (tag === 'posts') continue;
          if (!tagToItems.has(tag)) tagToItems.set(tag, []);
          tagToItems.get(tag).push(item);
        }
      }
    
      // For each tag, sort by date desc and chunk into pages
      for (const [tagName, tagItems] of tagToItems.entries()) {
        tagItems.sort((a, b) => b.data.__pubTs - a.data.__pubTs);
        for (let i = 0, pageNumber = 0; i < tagItems.length; i += paginationSize, pageNumber++) {
          const pageData = tagItems.slice(i, i + paginationSize);
          tagMap.push({ tagName, pageNumber, pageData });
        }
      }
    
      return tagMap;
    });
  24. zachleat commented on Nov 12, 2025

    @zachleat
    Member

    Home base for this is #3987

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions