Dynamic content and invalid html markup
Dynamic content is one of the new features in EPiServer CMS R2 and looks like a potential way of adding dynamic content to your XHTML-editors and keep control over the rendering of the markup.
In this short article I will not show you how to create dynamic content plugins, instead I will focus on the problem that the XHTML-editor always wraps the contents in paragraph tags.
So lets assume that you we have created a new dynamic content plugin that renders an h3, one image and a paragraph, if you add this new dynamic content in your editor and hit save and plublish you will see that the editor has wrapped your markup within a paragraph which invalids your markup.
There is maybe a better way of doing this but my suggestion is to take control over the property rendering by creating a new class that inherits from PropertyLongStringControl and override the CreateDefaultControls method like I did in this article. Below is some example code that shows how to do this.
public override void CreateDefaultControls() {
//ignore processing if no data exists
PropertyXhtmlString propertyData = PropertyData as PropertyXhtmlString;
if (propertyData == null) {
base.CreateDefaultControls();
return;
}
Control target;
//if we don't have any attributes we can suffice with a PlaceHolder
if (AttributeSourceControl.ControlStyle.IsEmpty) {
target = new PlaceHolder();
} else {
//else we need a panel and copy the attributes.
target = new Panel();
CopyWebAttributes((Panel)target);
}
Controls.Add(target);
foreach (IStringFragment fragment in propertyData.StringFragments) {
if (fragment is StaticFragment) {
string content = fragment.InternalFormat;
content = Regex.Replace(content, @"^\", "");
content = Regex.Replace(content, @"\$", "");
Literal literal = new Literal { Text = content };
target.Controls.Add(literal);
} else {
target.Controls.Add(fragment.GetControl((PageBase)Page));
}
}
}
This code also removes the extra div tag around the the editor to make the markup cleaner but I´m not completely satisfied with this solution but at the moment I can't figure out a better way, can you?
I wouldn't recommend not doing it the way you are doing. It forces the developer to make changes in the global.asax file, which isn't always possible. Especially if you are producing a plug-in or something that is to be distributed. A better way is to use PropertyDataControlAdapter and then set up the "override" in the browser capabilities file. Just an idea... If you don't like the adapter way, you could ccreate a class inheriting from PlugInAttribute and use its Start method to register your control. This will also solve the whole Global.asax problem...
Chris:
I really like to use adapters for this type of problems but in this case I totally forgotten about it so thank you for reminding me.
When is it not possible to make changes in the global.asax file?