New Snippet *** Truncate Strings (not removing HTML tags)
So here comes new snippet for our RAZOR lovers. I needed this for a client project. In the past i turned my back away from xslt. After testing with truncate helper and trim etc. i developed this solution, based on the MVC3 hints on ASP.NET. If somebody would suggest improvements, post it in here. Have a nice day.
@{ //GIVE ME THE ROOTNODE var start = Model.AncestorOrSelf(); } //CRAWL THROUGH MY NODES AND CHOSE ONLY THE ONES WITH DOCTYPE NEWS @foreach(var article in start.Descendants("News")) { var excerpt = article.newsText.ToString(); <a href="@article.Url" class="news_article"> <strong class="news_title">@artikel.title</strong> //YOU CAN COMBINE THIS WITH A MACRO PARAMETER TO DEFINE THE NUMBER AFTER CUTTING THE TEXT <span class="news_short"><p>@Truncate(excerpt, 300)</p></span> <span class="date">@artikel.date</span> </a> }
@{ //GIVE ME THE ROOTNODE var start =Model.AncestorOrSelf(); } //CRAWL THROUGH MY NODES AND CHOSE ONLY THE ONES WITH DOCTYPE NEWS @foreach(var article in start.Descendants("News")) { var excerpt = article.newsText.ToString(); <a href="@article.Url"class="news_article"> <strong class="news_title">@artikel.title</strong> //YOU CAN COMBINE THIS WITH A MACRO PARAMETER TO DEFINE THE NUMBER AFTER CUTTING THE TEXT <span class="news_short"><p><text>@Truncate(excerpt,300)</text></p></span> <span class="date">@artikel.date</span> </a> }
So just wrap the line with the @Truncate inside a <text></text> element. So it's ignoring the inside tags.
New Snippet *** Truncate Strings (not removing HTML tags)
So here comes new snippet for our RAZOR lovers. I needed this for a client project. In the past i turned my back away from xslt. After testing with truncate helper and trim etc. i developed this solution, based on the MVC3 hints on ASP.NET.
If somebody would suggest improvements, post it in here.
Have a nice day.
CODE:
UPDATE:
reason: the truncate is stripping of html tags at the end, because it's formatted to string and html.encoded.
So just wrap the line with the @Truncate inside a <text></text> element. So it's ignoring the inside tags.
is working on a reply...