TechLifeWeb

Note 2024-09-21T11:09:36.000-07:00


Published by 
Scott Kingery
 on 

Today's side quest is figuring out how to have a collection in #11ty that is sortable by title and lets me pass in a tag. This code below works but isn't dynamic in that the tag "garden" is hard coded. I can't seem to figure out how to make the tag part more flexible so I can use it anytime with other tags and not have to build different collections.

eleventyConfig.addCollection("myCustomSort", function(collectionApi) {
	return collectionApi.getFilteredByTag("garden").sort((a, b) => {
	  return a.data.title.localeCompare(b.data.title);
	});
  });

and then using it:

image of the code to run and 11ty collection