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:

   1:  namespace DanHounshell.Graffiti.Chalk 
   2:  {
   3:     [Chalk("RedirectIfSinglePost")]
   4:     public class RedirectIfSinglePost {
   5:        public void Redirect(PostCollection posts)
   6:        {
   7:           if (posts.Count == 1)
   8:           {
   9:              HttpContext.Current.Response.Redirect(posts[0].Url);            
  10:           }
  11:        }
  12:     }
  13:  }

 

Usage

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

   1:  $RedirectIfSinglePost.Redirect($posts)

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:

   1:  #if($where == "category")
   2:       <h2 class="archive_head">Posts in &gt;'$category.Name'</h2>
   3:  #elseif($where == "tag")
   4:       <h2 class="archive_head">Entries tagged '$tag'</h2>
   5:  #elseif($where == "search")
   6:       $RedirectIfSinglePost.Redirect($posts)
   7:       <h2 class="archive_head">Search Results for '$macros.SearchQuery'</h2>
   8:  #end

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, mobile, motorcycles, watches, food, Nashville, Savannah, Cincinnati and family. Dislikes: mean people
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