Sunday, 29 August 2010

Tip: Configuration settings on your website

Just a little tip today, but one I've had to use on all the N2 work I've done.

For most sites I've had to do, I've had to add general configuration settings that can be access by any page. The current N2 site has these - if you right click/edit on your homepage and then click on the "Site" tab, you'll see a few there such as 404settings and the like.

I like to put all my config values in here as it's then all in the one place, but accessing them is a bit of a pain. If you try to access them by N2.Find.StartPage then the object returned is a ContentItem, not a StartPage.

To get round this, I create a static class and have a static method to return any property in the configuration settings:

public static StartPage SiteConfigurationSettings
{
        get
        {
                StartPage sp = N2.Find.StartPage as StartPage;
                if (sp == null) 
                {
                        throw new Exception("Cannot find the N2 Start page");
                }
                else
                {
                        return sp;
                }

        }
}

Now you can then access this property across the site in code as well as allow the end users to be able to update the value!

This is just a simple bare-bones example, but of course, you can cache this property too :)

3 comments:

  1. Hi,

    I can't seem to find that Site tab on my homepage. Is this a config change or a recent addition to the CMS?

    Thanks.

    ReplyDelete
  2. After having a good look at the source code, the Site tab is specific to a LanguageRoot. If you don't have one, you won't see it.

    ReplyDelete