Reputation: 5712
I have a property which is a Rich Text Editor in Umbraco 7.
I want to get a part of the content of this property, to be more exact, the first p tag.
How can I accomplish this in Umbraco? Is their a helper class that I can use?
Upvotes: 0
Views: 380
Reputation: 1285
There isn't a helper method that you can use out of the box but it shouldn't be too difficult to write your own.
If you're using MVC then you could write an extension to the MVC HtmlHelper as follows:
public static string GetFirstParagraph(this HtmlHelper helper, IHtmlString input)
{
if (input != null && input.ToString() != string.Empty)
{
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(input.ToString());
var p = htmlDoc.DocumentNode.SelectSingleNode("//p");
if (p != null)
{
return p.InnerText;
}
}
return null;
}
To call this method in your view simply type:
@Html.GetFirstParagraph(Umbraco.Field("yourPropertyAlias"))
if using the Umbraco Field method, or:
@Html.GetFirstParagraph(Model.YourProperty)
if your view is strongly typed.
If you're actually using Web Forms then you could create a razor macro and use the code above to perform the same task.
Upvotes: 0