Hiding empty years in the Orchard blog archive list

Hiding years that don't have any associated blog posts is easier than it looks!

Because I had a bit of a stop/start relationship with my blog for the best part of a decade there are a lot of years where there aren't any posts. This kinda filled up the "Archives" list I've got on the right-hand side with empty entries for 2014, 2013, 2012 and 2010 which adds four years to the 10 where I've actually blogged (until I tried to tidy this away it felt/looked like there were more than four empty years!). Finally I got round to seeing if I could tweak a template (Orchard being amazingly flexible for allowing this sort of thing!) so that the Archives list wouldn't show years that didn't have any posts.

Working out what view to tweak is made very simple with Orchard by an awesome tool called Shape Tracing, by switching it on (it only shows up for logged in users, but don't leave it active by default) and using it to find the relevant bit of the page, so as to find out what template/view needs to be updated:

Orchard Shape Tracing showing the shapes that make up the blog archive p[art of the page

Once I've narrowed it down to a specific view, it's time to create a new copy of the view in my theme. By copying the view into my theme, rather than editing the one that sits inside the Orchard.Blogs module I protect myself against my changes being undone by an update to Orchard. Once I've copied the page into my theme (into the directory /Views/), preventing years that don't have any posts in them from appearing in the list is quite simple to achieve by tweaking the existing code. The content of the Parts.Blogs.BlogArchives.cshtml view as of the time of writing contains the following code that's responsible for rendering each previous year (lines 27 through 33):

if (year != lastYear) {
    <li class="previous">
        <h4>@year <span>(@yearMonths.Sum(ym => ym.Value))</span></h4>
        @Html.UnorderedList(yearMonths, (t, i) => Html.Link(Html.Raw(string.Format("{0} ({1})", 
monthNames[t.Key.ToDateTime().Month - 1], t.Value)),
Url.BlogArchiveMonth((BlogPart)Model.Blog, t.Key.Year, t.Key.Month)), "archiveMonthList") </li> }

This code generates an HTML list, with one entry for each month that exists within the list of post counts for each month within the year. In order to prevent any year where the number of posts comes to zero from showing, a small tweak to the first line of the snippet can be made:

if ((year != lastYear) && yearMonths.Sum(ym => ym.Value) > 0) {

Once this tweak is present, hey presto, years where there aren't any posts will no longer show up in the archive list. This doesn't prevent the (empty) archive pages for years/months that are now not being shown in the list (like this page for 2010) from being shown if users somehow end up on them, so nothing out there that's linked via search results or bookmarks will break.

About Rob

I've been interested in computing since the day my Dad purchased his first business PC (an Amstrad PC 1640 for anyone interested) which introduced me to MS-DOS batch programming and BASIC.

My skillset has matured somewhat since then, which you'll probably see from the posts here. You can read a bit more about me on the about page of the site, or check out some of the other posts on my areas of interest.

No Comments

Add a Comment