January 28, 2009

Customizing Graffiti: Chalk GetQuerystringValue

My last Customizing Graffiti post was a simple little thing. This one is just as simple - the GetQuerystringValue Chalk extension. I had a need for pulling a value from the querystring recently and using it my view markup. I was sure that something to do that must exist in Graffiti, but I looked and looked through the Graffiti docs and forums and came up empty.

 

What does it do?

You use it to pull a value out of Querystring parameter. That’s it. I’ve found this to be really useful to pull in a querystring value for use as a parameter in another Chalk extension method call.

The Code

 

 

   1:  namespace DanHounshell.Graffiti.Chalk 
   2:  {
   3:     [Chalk("QueryStringHelper")]
   4:     public class QuerystringHelper 
   5:     {
   6:        public string GetQuerystringValue(string key) 
   7:        {
   8:           if (HttpContext.Current.Request.QueryString[key] != null)
   9:           {
  10:              return HttpContext.Current.Request.QueryString[key];
  11:           }
  12:           return string.Empty;
  13:        }
  14:     }
  15:  }

Usage

Just place the following wherever you need it in your view markup:

   1:  $QuerystringHelper.GetQuerystringValue("SomeQuerystringKey")

An entirely fictional “real world” example: I have a chalk extension that uses the Ebay API to get a list of the top 5 “going soon” auctions that match a specific keyword. I’m thinking about adding this to the bottom of every post on my site by including it in my post.view markup. The chalk method call inside my view pages might look like this:

   1:  $EbayService.GetAuctions("some keyword", 5)

I could always hardcode a keyword into the method call, but that wouldn’t be very useful. Or I could create a custom field for each post that is used as the keyword for the method call when that post is displayed. That might be a little better and could serve as the default. But let’s go a little further. If someone searched for a term on my Graffiti site, wouldn’t it be cool if I could use the term that they used to pull back Ebay auctions that match that term. I could see that being useful!

So in my index.view file I use conditionals to determine if it is displaying search results and if so then I append a querystring value like “searchedfor=thekeyword” to the Url of posts displayed in the search results. Then in my post.view I add something like this:

   1:  #if ($QuerystringHelper.GetQuerystringValue("searchedfor") != "")
   2:     $!EbayService.GetAuctions($QuerystringHelper.GetQuerystringValue("searchedfor"), 5)
   3:  #else
   4:     $!EbayService.GetAuctions($!post.Custom("EbaySearchTerm"), 5)
   5:  #end

 

When they click on a link in the search results they go to a post with the querystring appended. Now I can display items that match the search term they used previously or default to a custom term that I specified when I wrote the post if none is present.

Okay, I admit I took the long way around to create a complex contrived example to demo a piece of code that is so simple, but I promise that I will post a really good example that makes use of all three of my recent Graffiti tips later this week. 

Notes

After demoing this code to Jayme, he said something like “We already have that in Graffiti, but it is not documented very well”. He said that you can do the same thing with something like $request.QueryStringKey (where QueryStringKey = key you are trying to get the value of). There’s nothing like re-inventing the wheel!

Be careful, the above extension returns the raw querystring value without any html encoding or parsing of bad stuff. Make sure you take any necessary precautions before writing the value out to HTML, passing it to a database, etc.

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

Comments,

  • Trackbacks,
  • and Pingbacks

Comments are closed.

 

Trackbacks and Pingbacks


  1. In my last post, Customizing Graffiti: Chalk GetQuerystringValue , I promised that I was brewing up a

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