Writing a custom Chalk extension for GraffitiCMS

Wednesday, April 20, 2011

If you are not familiar with it, GraffitiCMS is an ASP.Net C# Content Managment System from Telligent that recently (last month) went open source. I was not at all familiar with the product until last month, but since it has gone open source I have jumped in with both feet as part of my quest to find the 'perfect' CMS, and also as a to use as a building block for a vertical product I have in the works.

So far, I have become quite enamored with GraffitiCMS. The code is sweet, easy to understand, and there is only about 15K lines of it - very easy to wrap your mind around it - and yet gives you a decent blog platform, and a very easy framework to extend for custom purposes. Better still, within 2 hours of downloading it, I had it up and running and was already writing extensions in C# that could drop right in to the application to extend it.

Anyway, a question popped up on the codeplex discussion board, and the question was asked:

 

I would like to show something on page only, if it's older than one month - so how to do that? For example:

#if($post.Published <= $today.AddDays(-20))
<h1>show something</h1>
#end

Any ideas?

 

If you haven't written a chalk extension, it is easier than you think. (If you need more of an overview on Chalk, check the GraffitiCMS website)

There are 3 basic steps to writing your own extensions:

  1. Create a .NET class library and add a reference to Graffiti.Core.dll  and any other assemblies you may want to use.
  2. Add a ChalkAttribute decoration to the class, and give it a name. In the example below it is the   [Chalk("today")] line
  3. Compile the project and drop the resulting .dll into the /bin folder of your blog site, and that is it!

Here is the meat of the code you will need for the .Net Class library you will create:

using System;
using Graffiti.Core;

namespace GraffitiExtras.Extensions.Today
    
{
    [Chalk("today")]
    public class today
    {
        public DateTime AddDays(int days)
        {
            return DateTime.Today.AddDays(days);
        }
    }
}

Now this is not necessarily a complete solution - I didn't put much thought into what other methods I like to expose, or naming convention to use, but only addressed the question asked. And as you can see with approximately 10 lines of code, compiled into a Graffiti Extension, adding functionality that was not there before is a very easy thing to do.

This is one of the reasons that the more time I spend working with this code, the more I like it.

 

Filed Under: GraffitiCMS, Chalk