Implementing a blog/archive page


Published: 7/4/2012, Author: Håkan Edling Categories: Tutorials, Piranha, 0 Comments

In this post we'll look at how to implement a startpage for a blog with archive functionality with Piranha. We'll start off by implementing this functionality only with inline page code, and in the next article we will continue by refactoring it by moving the code into a custom page implementation and extend the basic PageModel.

Almost every site need some kind of archive functionality, weather it's a blog or a news archive. Luckily for us, there's a lot of base functionality for this included in Piranha, and as an example we'll look at how we've implemented the blog page at this site.

First of all we have to decide which posts we want to show on this archive page, in our site we have a Post type called Blog post and these are the posts that we want to display. Let's start off by getting all of those posts from the database ordered by the time they were published.

@{
List<Piranha.Entities.Post> posts = null ;

using (var db = new Piranha.DataContext()) {
posts = db.Posts.
Include("CreatedBy").
Include("Categories").
Where(p => p.Template.Name == "Blog post").
OrderByDescending(p => p.Published).ToList() ;
}
}

What we're doing here is basically loading all of the blog posts and then telling Entity Framework to include some extra stuff for us that we want to display together with the posts in our archive. Now we can display these posts the way we want in our view, for example:

<div class="posts">
@foreach (var post in posts) {
<article>
<header>
<h2><a href="@UI.Permalink(post.PermalinkId)">@post.Title</a></h2>
<span>
Author: @post.CreatedBy.Firstname @post.CreatedBy.Surname
</span>
<span>
Categories: @post.Categories.Select(c => c.Name).ToArray().Implode(",")
</span>
</header>
<p>@post.Excerpt</p>
<p>Published: @post.Published.ToShortDateString()</p>
</article>
}
</div>

Allright, now we have all of our blog posts showing up in our page, but we also want to be able to get an archive for a specific month or year by adding these two parameters to the url, for example http://site/blog/2012/6 should give us all of the blog posts for june 2012. Let's modify our code by looking for this data in the url.

@{
int? year = null, month = null ;

if (UrlData.Count > 1) {
try {
year = Convert.ToInt32(UrlData[1]) ;
} catch {}
}
if (UrlData.Count > 2) {
try {
month = Convert.ToInt32(UrlData[2]) ;
} catch {}
}

List<Piranha.Entities.Post> posts = null ;

using (var db = new Piranha.DataContext()) {
var query = db.Posts.
Include("CreatedBy").
Include("Categories").
Where(p => p.Template.Name == "Blog post") ;

if (year.HasValue && month.HasValue) {
var start = new DateTime(year, month, 1) ;
var stop = start.AddMonths(1) ;
query = query.Where(p => p.Published >= start && p.Published < stop) ;
} else if (year.HasValue) {
var start = new DateTime(year, 1, 1) ;
var stop = start.AddYears(1) ;
query = query.Where(p => p.Published >= start && p.Published < stop) ;
}
posts = query.OrderByDescending(p => p.Published).ToList() ;
}
}

So now we're also looking for year and month in the url and we've updated our query to use these parameters to filter our posts accordingly. In the next post we'll refactor this page by moving the code into a custom page implementation and creating a custom model extending the basic PageModel. We'll also add archive functionality showing the available posts by month to navigate through the archive.


blog comments powered by Disqus