January 18, 2009

Customizing Graffiti: Chalk RedirectIfSinglePost

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.

Comments,

  • Trackbacks,
  • and Pingbacks
  1.  avatar Adonis says:

    Great tip!  I'm going to have to add this to my new theme!  Thanks Dan.

  2.  avatar Alex Crome says:

    This is fantastic.  I'm going to have to add this to my blog shortly.

  3. 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.

Comments are closed.

 

Trackbacks and Pingbacks


  1. Continued Graffiti goodness. Here is another Graffiti customization - the GetAppSetting Chalk extension

Shortcuts

Where is Dan?


My Blog
My Blog
Twitter
Twitter
Facebook
Facebook
LinkedIn
LinkedIn
Flickr
Flickr
YouTube
YouTube
Delicious
Delicious
Foursquare
Foursquare
Pinterest
Pinterest
GetGlue
GetGlue
Pintley
Pintley
XBOX Live
XBOX Live
Last.fm
Last.fm
Windows Live
Windows Live
Telligent.com
Telligent
Graffiti CMS on CodePlex
Graffiti CMS
Popular

Recent Posts