I have a textbox multiple property on my site. In this field there are some line breaks. I'd like to show these line breaks on the rendered content to the users.
In XSLT I would just use the ReplaceLineBreaks extension and put a disable-output-escaping="yes" on the value-of.
But now I have to do this in Razor.
I don't have the property on the current page, but on another page. I'm grabbing the right node like this:
If you'd like a solution that is a little more clean. Drop the following code into your App_Code folder or add it to your project:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using umbraco.interfaces;
using umbraco.MacroEngines;
using Umbraco.Core.Models;
namespace StraylightDk
{
public static class Extensions
{
//Extension for the old Razor Macroscripts model
public static HtmlString Nl2Br(this System.Web.WebPages.Html.HtmlHelper html, IProperty property)
{
return Nl2Br(property.Value);
}
//Extension for the new Mvc Razor model
public static HtmlString Nl2Br(this System.Web.Mvc.HtmlHelper html, IPublishedContentProperty property)
{
return Nl2Br(property.Value.ToString());
}
private static HtmlString Nl2Br(string value)
{
string result = HttpUtility.HtmlEncode(value);
result = Regex.Replace(result, "(\r\n|\n)", "<br />$1");
return new HtmlString(result);
}
}
}
ReplaceLineBreaks in Razor?
Hi guys
I have a textbox multiple property on my site. In this field there are some line breaks. I'd like to show these line breaks on the rendered content to the users.
In XSLT I would just use the ReplaceLineBreaks extension and put a disable-output-escaping="yes" on the value-of.
But now I have to do this in Razor.
I don't have the property on the current page, but on another page. I'm grabbing the right node like this:
This gives me the content fron the textbox multiple, but without any line breaks.
So what do I have to do to get the line breaks in the rendered content using the above snippet?
Thanks in advance!
/Kim A
You can still use the same extension:
Hi Sebastiaan
Thanks for your answer.
I'm getting an error when I try you snippet. There's a red line under this part:
When hovering in Visual Studio it says this:
You have any idea to why this happens?
/Kim A
Ah yes, that should be termsPage.GetProperty("manchet").Value
And maybe you then need to rewrite it to:
And as an update this looks a little nicer:
Great Sebastiaan!
This caused some kind of error as well:
But this snippet works like a charm:
@Html.Raw(umbraco.library.ReplaceLineBreaks(termsPage.GetProperty("manchet").Value))
Thanks a lot.
See you friday :)
/Kim A
If you'd like a solution that is a little more clean. Drop the following code into your App_Code folder or add it to your project:
Now you can write your razor script like this:
This seems like a more "clean" way to do it.
Ahh that was a nice approach as well Michael.
I'm not that strong in C#, but looks like a nice method to use for a frontend'er like me :)
See you friday as well Michael!
/Kim A
Hi Michael,
Thanks for this Extension Method. I tweaked it a little:
//Extension for the new Mvc Razor model
public static HtmlString ReplaceLineBreaks(this IPublishedContent content, string alias)
{
return Nl2Br(content.GetProperty(alias).Value.ToString());
}
private static HtmlString Nl2Br(string value)
{
string result = HttpUtility.HtmlEncode(value);
result = Regex.Replace(result, "(\r\n|\n)", "<br />$1");
return new HtmlString(result);
}
Now I can use in my Partial View Macro File like this:
@Html.Raw(Model.Content.ReplaceLineBreaks("mediaArticleSummary"))
Greetings,
Anthony
is working on a reply...