Mark Oliver's World

Posted: 03/05/2021

Adding Category Pages

Now we have tags on posts, it is easy to see all related posts, but there is no way to access a list of the known categories/tags.

So lets add one.

What do we need?

A new page "Category List" needs to be created.
This page will query the blogpostpopulater for the list of categories and post names read from the "index".

Then we will use the Virtualize built in Blazor component to work through the returned data, and add a TagsDisplay component we built previously to show the tags.

Had to transform the data from a list of BlogInfos to a Dictionary of Lists of BlogInfos keyed on categories.
This is probably easy to do in Linq, but my LinqFoo is not strong in that area, so a nested set of forloop to populate the list is fine (Please, let me know if you can do it linq!):

            Dictionary<string, List<BlogInfo>> result = new Dictionary<string, List<BlogInfo>>();  foreach (var post in allPosts.Where( t => t.Tags != null )) {     foreach (var tag in post.Tags)     {         if (result.ContainsKey( tag ))         {             result[tag].Add( post );         }         else         {             result[tag] = new List<BlogInfo> { post };         }     } }
            
          

So once thats all available, I just need to put it all together with a bit of styling and using the exiting components for displaying titles and links, and we get this "page", which I quite like.

Thinking about it, I also need to add it to the NavBar too, so a nice new icon here.

Im thinking of dropping the latest 10 blog posts from the nav bar too, its getting a bit crowded. Perhaps just last 3.

One last think, I need to sort the categories alphabetically. They help the reader scan them more quickly- There is no other reason than that, as they are all unrelated.


Thanks for reading this post.

If you want to reach out, catch me on Twitter!

I am always open to mentoring people, so get in touch.