Graffiti CMS Chalk Extension for Beginners

Writing Chalk Extensions is very simple.  As mentioned here, to extend the functionality of Graffiti CMS you simply need to add a reference to Graffiti.Core.dll, add the Chalk Attribute to the class that you will be extending and put the compiled .dll in the bin directory of your web directory.  It is important to note that creating extensions implies that you are using Visual Studio or some other .net code compiling tool.   A couple of other notes are:

  • Only public methods are exposed.
  • Only public properties are exposed.

Here is a copy of a C# class file (ChalkExtensionBeta.cs) that I created.  This is a complete code sample that you can copy, paste, compile and test on your installation.  Before I paste the code sample I want to highlight some important aspects of the sample:

First, notice the class attribute [Chalk(“ChalkExtensionsBeta”)].  Since I declared my attribute as ChalkExtensionsBeta when I want to use this code in layout.view, or any other .view file, I will access the functions enclosed by $ChalkExtensionsBeta.  Looking at the code below I have one function exposed in the chalk extension $ChalkExtensionsBeta.  Namely, AddCookie() which will add a cookie with the name and value that you specify.  Putting it all together, once you have compiled this code and copied the .dll file to your website’s bin directory, you could use this in your layout.view file as follows:

$ChalkExtensionsBeta.AddCookie(“CookieName”,”CookieValue”)

That’s all there is to it.  This is a very simple extension to get you started, but these extensions can be very complex when needed.  My final tip for creating Chalk Extensions is to be sure that someone else hasn’t already developed the extensions you want to write.  There are two fantastic libraries on CodePlex, ChalkExtensions and GraffitiExtras, that have already started to build a collection of highly useful extensions.  Check these out first and search for other already developed extensions before you start writing your own.

using Graffiti.Core;
using System;
using System.Collections.Generic;
using System.Web;

namespace Chalk.Extension
{
[Chalk(“ChalkExtensionsBeta”)]
class ChalkExtensionsBeta
{
public void AddCookie(string cookieName, string cookieValue)
{
HttpContext context = HttpContext.Current;
context.Response.Cookies.Add(new HttpCookie(cookieName, cookieValue));

}
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *