11ty, nunjucks, tag counts
As I write more for the 100DaysToOffload, I like to post on my social media the post number. I also made this page for myself to review the posts Iβve written, and post counts. Initially I did not have this added on this page, but it is on my tags page where I list the tag and the number of posts in it.
On my tags page, it uses an eleventy filter of the collection of all posts, and adds this information to the collection. Then on the tags page do a for loop to loop through all the tags and post counts with Nunjucks.
config.addCollection("tagList", collection => {
const tagsObject = {}
collection.getAll().forEach(item => {
if (!item.data.tags) return;
item.data.tags
.filter(tag => !['post', 'all'].includes(tag))
.forEach(tag => {
if(typeof tagsObject[tag] === 'undefined') {
tagsObject[tag] = 1
} else {
tagsObject[tag] += 1
}
});
});
const tagList = []
Object.keys(tagsObject).forEach(tag => {
tagList.push({ tagName: tag, tagCount: tagsObject[tag] })
})
return tagList.sort((a, b) => b.tagCount - a.tagCount)
});
This is great, but I have a simpler collection of the posts by tags using the collectionApi.getFilteredByTag()
filter.
// Get only content that matches a tag
config.addCollection("offload", function(collectionApi) {
return collectionApi.getFilteredByTag("100DaysToOffload").reverse();
});
So now on my page specificly for the 100DaysToOffload, we can do a for loop to display just the posts in this collection.
Get IP From DNS Name Using Python
Updated Plop Automation 2.0
Human Readable File Sizes in PowerShell
Reverse, esreveR
Switch Python to Match
Too Much Data
Confirming public GPG fingerprints from Git platforms
AWS EC2 termination protection
Burning musical memories
100 Pics - Day 9
Securing 11ty with SSL Locally
100 Pics - Day 8
Adding GPG to Codeberg From Windows
Codeberg Migration
Jellyfin and SSL
Be Proud of Your Work
Blog Questions Challenge 2025
Changing Your LUKS Full Disk Encryption Password
How to tag AWS EBS volumes on Linux EC2 Servers
100 Pics - Day 1
Reviewing 2024
Year in Review
Updating Codeberg Pages Static Sites
Expanding URLs in Powershell
Automation waiting game
The Collection
Mobile writing, part 2
Getting into Podcasts
Using PSCustomObjects
Backing up the Cloud
Future proofing scripts
The state of Linux
Making Notepad with HTML
Updating Docker Containers
Advent of Code - Day 4
Changing Git config URL
Advent of Code - Day 1
Wordle Analytics - November 2023
Reducing the noise
Making Change
Knowing the long way
Write fast, deploy, update
Migrating 2FA apps
Hardware and Tech I Use
Do you follow RSS?
GNOME VS KDE
Almost there - 100DaysToOffload
Download latest file from a S3 Bucket
Posting from my phone
Default apps
Git Status
Lessons learned, backup
Installing Node.js on Pop!_OS 22.04 LTS
Wordle Analytics - October 2023
Using ChatGPT wisely
Simple HTTP Server
My Linux Journey
Regex a UNC path
TinaCMS + 11ty
Verifying Ubuntu 23.10.1 ISO
Updating Umami Analytics
Taking A Screenshot In Python
100 Days to Offload Update
Network Usage Since Last Boot
Hosting FreshRSS on Fly.io
Robots and AI
File Encryption with GPG
Retro Gaming
Creating a Test File
Wordle Analytics - September 2023
Using Cheat sheets
Creating Status Pages on the Fly
Scheduling Automatic Builds with Static Site Generators
Boto3 cross regions for AWS Lambda Functions
Building a Blogroll with 11ty
Adding some flare to RSS
List Throttling with Python
Migrating GPG keys to a new machine
Installing Node.js on Fedora
CloudCannon + Eleventy
The Good Side of Analytics - Umami & Vercel
Wordle Analytics - August 2023
Manage AWS Instances
Wordle Analytics - July 2023
I Broke NextCloud, Again
What is in Your Headers?
Wordle Analytics - June 2023
Mapping the internet
Wordle Analytics
Finding your Windows Install Date
Powershell GUI with WPF
Ubuntu's change with Python support
Bookmarks in PowerShell
Console Speech API, Part 2
Console Speech API and Beeps
Spring Cleaning
Getting Into Cron Jobs
Intro to Pickleball
Using the Codeberg CI
How I Missed Gaming
Using Cloudflare Workers to Get Visitor Information
Select-Object with PowerShell
Command Line vs GUI Windows programs
Plop in Automation
Extracting Files with Progress
Port Checking in Python
Creating a static site using Python and Flask
Going Headless
Nextcloud Docker Compose
11ty, nunjucks, tag counts
Adding a basic search to a static site
Getting started with docker and Luanti (Minetest)
GitHub profile with actions
Website redesign with 11ty!
Quick tips on Linux aliases
How to start coding
Securing Jekyll with SSL locally
Got Git?
Netlify hosting and redirects
Working with Tags in Jekyll
New Year's Resolution for 2023
This solution does not have a built in way to get the tag count. So I found two solutions that work in this situation.
Solution 1
Using the full tags collection. For this to work, we need to loop over all the tags, and if we find the tag we want, pull out the tagCount that is defined in our collection.
<h1>100 Days To Offload
</h1>
Depending on how many tags you have youβd still have to loop over all of them to pull out the one. This is a collection that is more of an array of the data that would be needed in one object.
Solution 2
The second method uses a variable and a loop as well to increase a count, then output that count.
<h1>100 Days To Offload
<small>(121 posts)</small>
</h1>
I like this method as I have already filtered the tag in a collection, and manually added the count on the page at render time. This is a collection that just has the posts and basic information and not the extra details that could be included.
Wrap up
Both methods do work and could still have a valid use case when needed. My tags page has its own loop process to notate the tag name and tag counts. Now as I post more to the 100DaysToOffload, I can know the post counts. The 100DaysToOffload page uses the simpler collection and a for loop to get the data that is needed. On this page, Iβd rather call back to the same collection, than call a larger collection to get a smaller amount of data.
When I moved from Jekyll and talked about the 11ty redesign, I had a post about the same process in Jekyll and working with tags
Reply via email