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 existsPropertyXhtmlString propertyData = PropertyData as PropertyXhtmlString;if (propertyData == null) {base.CreateDefaultControls();return;}Control target;//if we don't have any attributes we can suffice with a PlaceHolderif (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, @"^\</p\>", "");content = Regex.Replace(content, @"\<p>$", "");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?