all posts

Customizing Graffiti Chalk RedirectIfSinglePost

Published to Blog on 18 Jan 2009

In my last post I promised a wealth of Graffiti posts would soon be flowing from my finger tips. Here is my first and probably my favorite Graffiti customization - the RedirectIfSinglePost Chalk extension. I have used this on every Graffiti site I’ve put together or toyed around with.

What does it do?

If you place a call to this Chalk extension at the top of your index.view page then if there is only one post in the category, search results, whatever, instead of just displaying that one post in the index.view it will instead redirect directly to that post.

The Code

Very simple:

namespace DanHounshell.Graffiti.Chalk
  public class RedirectIfSinglePost {
    public void Redirect(PostCollection posts)
      if (posts.Count == 1)


Just place the following at the top of your index.view page:


One warning: If you do not have a separate home.view (you’re using index.view on your home page) and only have one post on your site then this will cause your site to redirect to that one post. This may not be what you intend.

You may also want to play around with the placement of the call to Redirect(). You can try mixing into if statements to check if the index page is a category page, a tag page, or a search results page and only redirect in specific instances. For example if you only wanted to redirect on search results pages but not on tag results pages or category pages you could try something like this:

#if($where == "category")
  <h2 class="archive_head">Posts in &gt;'$category.Name'</h2>
#elseif($where == "tag")
  <h2 class="archive_head">Entries tagged '$tag'</h2>
#elseif($where == "search")
  <h2 class="archive_head">Search Results for '$macros.SearchQuery'</h2>

I hope you find this Graffiti Chalk extension as useful as I have. Please leave a comment if you have any suggestions or questions.

Dan Hounshell
Web geek, nerd, amateur maker. Likes: apis, node, motorcycles, sports, chickens, watches, food, Nashville, Savannah, Cincinnati and family.
Dan Hounshell on Twitter

  • On 19 Jan 2009 "Adonis"" said:
    Great tip! I'm going to have to add this to my new theme! Thanks Dan.
  • On 19 Jan 2009 "Alex Crome"" said:
    This is fantastic. I'm going to have to add this to my blog shortly.
  • On 19 Jan 2009 "Dan Hounshell"" said:
    Ado and Alex, I'm glad you like it. If you're like me, you'll find it indespensible and never leave home without it.
  • On 19 Jan 2009 "Digging My Blog - Dan Hounshell"" said:
    Continued Graffiti goodness. Here is another Graffiti customization - the GetAppSetting Chalk extension