More on CSModules

James Shaw wrote a great article telling you how you can do things like include adverts in your posts before they are rendered. I thought I’d just add something to that which people don’t always know. When doing things like adverts, you might not want to include them in the RSS feed (although your probably would ) or you might now want them to show up if your posts are sent out via email (i.e. using Mail Gateway for CS).

In the ‘private void csa_PreRenderPost(IContent content, CSPostEventArgs e)’ method, you can use e.Target to check where the post is being displayed. So, if you wanted to only display adverts for posts displayed on your website, you could simply do the following check.

if (e.Target == PostTarget.Web)

        {

            // Do your advert code here....

        }

Nice and simple, huh? It’s actually more useful in other scenarios where you want to do some processing on your posts before you display them, but it only makes sense to do it before you display it on a specific medium. For example, you could write a CSModule to render your smilies, but you probably wouldn’t want it to run for and offline medium as it couldn’t display the images.

2 comment(s) so far

admin avatar

Another good reason to check the target is if you have HttpContext dependendies.  In some things like NNTP or Mail Gateway, there is no HttpContext, so it would cause it to throw an exception.

admin avatar
wrote on January 23, 2008

Cool, great tip Ken, thanks!

Post your comment