Reputation: 7490
I have a Umbraco script Im using on a site, inside it there is a razor script as below:
<p>@page.GetProperty("mainContent")</p>
The above is in a loop, and shows content for each post (its being used on a landing page with blog like functionality)
I want to trim the content outputed by the GetPropery() method to say 300 charectors.
Anyone have any ideas?
Also, what word is the opposite of concatenate?
Upvotes: 0
Views: 5016
Reputation: 1038720
You could write a custom helper:
public static class HtmlExtensions
{
public static string Truncate(this HtmlHelper html, string value, int count)
{
if (string.IsNullOrEmpty(value))
{
return string.Empty;
}
if (value.Length > count)
{
value = value.Substring(0, count - 1) + "...";
}
return value;
}
}
which could be used like this:
<p>@Html.Truncate(page.GetProperty("mainContent"), 300)</p>
Also, what word is the opposite of concatenate?
Split
Upvotes: 2
Reputation: 4257
The Umbraco Helper already has a method to do this for you! Calling
@Umbraco.Truncate(page.GetProperty("mainContent"), 300)
would do that for you out of the box, no need to write an extra extension method. It also has some extra overloads that allow you to specify extra behaviours (like adding an ellipsis to the end of the truncated string).
Upvotes: 12