Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Kim Andersen 1447 posts 2196 karma points MVP
    Apr 06, 2013 @ 20:08
    Kim Andersen
    0

    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:

    @{
        DynamicNode termsPage = Model.NodeById( Model._termsLink.ToString() );
        <div class="manchet">
            <h2>@termsPage.GetProperty("manchet")</h2>
        </div>
    }

    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

     

  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Apr 06, 2013 @ 20:21
    Sebastiaan Janssen
    0

    You can still use the same extension:

    @umbraco.library.ReplaceLineBreaks(termsPage.GetProperty("manchet"))
  • Kim Andersen 1447 posts 2196 karma points MVP
    Apr 06, 2013 @ 20:29
    Kim Andersen
    0

    Hi Sebastiaan

    Thanks for your answer.

    I'm getting an error when I try you snippet. There's a red line under this part:

    termsPage.GetProperty("manchet")

    When hovering in Visual Studio it says this:

    Argument type 'umbraco.interfaces.IProperty' is not assignable to parameter type 'string'

    You have any idea to why this happens?

    /Kim A

     

  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Apr 06, 2013 @ 20:35
    Sebastiaan Janssen
    101

    Ah yes, that should be termsPage.GetProperty("manchet").Value

    And maybe you then need to rewrite it to:

    @Html.Raw(umbraco.library.ReplaceLineBreaks(termsPage.GetProperty("manchet").Value))

     

  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Apr 06, 2013 @ 20:36
    Sebastiaan Janssen
    0

    And as an update this looks a little nicer:

    @Html.Raw(umbraco.library.ReplaceLineBreaks(termsPage.Manchet))
  • Kim Andersen 1447 posts 2196 karma points MVP
    Apr 06, 2013 @ 20:40
    Kim Andersen
    0

    Great Sebastiaan!

    This caused some kind of error as well:

    @Html.Raw(umbraco.library.ReplaceLineBreaks(termsPage.Manchet))

    But this snippet works like a charm:

    @Html.Raw(umbraco.library.ReplaceLineBreaks(termsPage.GetProperty("manchet").Value))

    Thanks a lot.

    See you friday :)

    /Kim A

  • Michael Wulff Nielsen 11 posts 74 karma points
    Apr 07, 2013 @ 09:52
    Michael Wulff Nielsen
    1

    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);
            }
    
        }
    }

    Now you can write your razor script like this:

    @using StraylightDk
    @{
        DynamicNode termsPage = Model.NodeById( Model._termsLink.ToString() );
    }
    
    @Html.Nl2Br(termsPage.GetProperty("manchet"))

    This seems like a more "clean" way to do it.

  • Kim Andersen 1447 posts 2196 karma points MVP
    Apr 07, 2013 @ 13:43
    Kim Andersen
    0

    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

  • Anthony Candaele 1197 posts 2049 karma points
    Sep 13, 2013 @ 09:58
    Anthony Candaele
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft