Sitefinity Development: Content Placeholders

by Stephen Horn Feb 24, 2015

Developers, I'm here to tell you Visual Studio really is a great tool.  The Telerik Sitefinity team(s) have embraced it and written some great plug-ins for it; Thunder is one such example.  One of the reasons I love Visual Studio is it has so many great shortcuts, that help productivity. But if we are not careful, a few of these shortcuts can lead to long term maintenance issues if not understood. One such example is the default master file structure, specifically, defaulting to putting a content placeholder (CPH) in the head tag.

Why is this such a big deal?  Why would I write a blog on this topic?  Why would anyone put a CPH in the head tag anyways?  Let me answer them in reverse order.  

The answer to why anyone would put a CPH in the head tag is, "I don't know."  Perhaps in some design patterns this makes sense. What I DO know, however, is that in Sitefinity development, it does not, and in fact it can hurt!  I write this blog because the best lessons in life come from failure; we hurt, but we learn.

Recently, during a migration on a site I was not that familiar with, I made a change to a master page that I thought was a sound one and that I was sure I could quickly resolve by then editing just two templates. But what I found when I got to the templates was that the controls I wanted to move were gone!  Because I know better, and I never put CPHs in the head tag, I had not thought to ensure there were none in this master page before failed notice a CPH in the head tag. So why is it such a big deal in Sitefinity Development?

cph-1
   IMG: Default Visual Studio Master Page

The short answer should make this obvious: If the master file of an existing template/page is modified, and a contentplaceholder is removed, then any Sitefinity controls that existed in that CPH will be placed in the first content placeholder in the flow of the master page! I think you can quickly see where this leads. If the first contentplaceholder is in the head tag then this is where Sitefinity will 'shelter the homeless controls'.  You just spent so much time creating all of those nifty Shared Content Blocks, so why would you want to lose them? You wouldn't of course, hence this blog post, to ensure you don't!

cph-2
   IMG-2: Something odd, only one content placeholder showing!

cph-3
   IMG-3: Let's add content anyway...

cph-4
   IMG-4: Oops! Someone removed the content placeholder in the master file!

Okay, so you will not actually lose them, you will just have to find some creative ways to get them back! Since Sitefinity doesn't give you access to the CPH in the head, just edit the master file and move it to just after the opening Body tag.

Note: If you still have the editor open, and you undo your change, as long as the CPH still has the original ID, your template/page will self-correct. This is at least true of SF 7.x and later.

Certainly sound design helps to prevent all of this jumping through hoops, and most likely you weren't considering putting a CPH in the head tag anyway, but it's still something to keep this in mind anyway as you might one day find yourself working with a project in which you were not the original designer. Often the original developer wasn't being malicious, or even careless; he may simply be the victim of using the tools that are supposed to help!  Don't be a victim; keep content placeholders out of the head tag when working with Sitefinity!

New Call-to-action