Once in a while the question how to strip <p> and <span> tags in empty placeholders comes up in the newsgroup.
A solution would be to implement a custom placeholder control that removes these tags or a workflow event.
The simplest solution is a workflow event to do this. This event would have to check if there is meaningful content in the control and if not ensure that an empty string is set a placeholder content.
Something like this:
public void CmsPosting_PlaceholderPropertyChanging( Object sender, PropertyChangingEventArgs e )
{
if (e.Target is HtmlPlaceholder)
{
if (IsEmpty(e.PropertyValue))
e.PropertyValue = “”;
}
}
private bool IsEmpty(string content)
{
//visible tags
string tagsToKeep = “img|hr”;
//This regular expression matches all tags listed above
string regExpTagsToKeep =
“<[\\s|/]*(“ + tagsToKeep + “)\\b[^>]*>”;
Regex reTagsToKeep = new Regex(regExpTagsToKeep, RegexOptions.IgnoreCase | RegexOptions.Compiled);
//Check if one of the tags to keep is included and exit.
if (reTagsToKeep.IsMatch(content))
{
//Placeholder is not empty.
return false;
}
//This regular expression gets all tags in the content
string regExpForAllTags = “<[^>]*>”;
Regex reAllTags = new Regex(regExpForAllTags, RegexOptions.IgnoreCase | RegexOptions.Compiled);
//Remove all Tags by replacing with an empty string
content = reAllTags.Replace(content, “”);
//Remove all spaces and non-breaking spaces ( )
content = content.Replace(” “,“”);
content = content.Replace(“ ”,“”);
if (content == “”)
{
//All tags removed, we are left with an empty string
//Placeholder is empty.
return true;
}
else
{
//After removing all tags, we still have content.
//Placeholder is not empty.
return false;
}
}
Permalink
Hey, granted this is older code, but any help here would be appreachated.
I need to do this to over come a bug, but we are useing VB.net and so far nothing has worked. I have tried to use your code, but cannot find out what to include to make it all work.
Thank you in advance
Permalink
Hi Eric,
you need to create a custom placeholder control, derive from the original one and add the above code to it.
Sorry I cannot help you with VB.NET. Never used it. You either need to create the placeholder control in C# or port the above code to VB.NET.
Cheers,
Stefan