x First time here? Check out the FAQ

Come work for Umbraco - The Umbraco HQ are hiring Project managers, .NET developers and DevOps people!

  • Avatar33posts57karma

    Remove paragraph tags with razor

    Markus started this topic April 12, 2011 @ 03:41, Go directly to the topic solution

    Hi,

    With "classic" umbraco, I can strip the surrounding paragraph tags

    <umbraco:Item field="myfield" stripParagraph="true" runat="server"></umbraco:Item>

    Can I do that with razor, too, when calling @Model.myfield ?



  • Rich Green posted this reply April 12, 2011 @ 03:51

    Not tested

    @umbraco.library:RemoveFirstParagraphTag(Model.myfield)

     

    Rich


  • Sebastiaan Janssen posted this reply April 12, 2011 @ 03:54

    That's unfortunately not possible. You could try to do something like:

    @{
      var field = Model.myField;
      field = field.Trim();
      field = field.Substring(3);
      field = field.Substring(0, field.length - 4);
    }

    I don't expect this to always work very well and the best solution would be to use HTML Agility Pack to do this more consitently.


  • Avatar33posts57karma
    Comment with ID: 73590
    Markus posted this reply April 12, 2011 @ 04:11

    The following based on Rich's (accepted) answer works:

    @umbraco.library.RemoveFirstParagraphTag(Model.myfield.ToString())

    Of course the elegance is gone ... :-(

     

    Thanks!


  • Sebastiaan Janssen posted this reply April 12, 2011 @ 04:26

    Ah yes, it wasn't the surrounding paragraph tag, just the first one! 

    Still pretty elegant, and at least it's not some proprietary property, but an actual C# method so that anybody reading it can understand what it does! :)


  • Avatar33posts57karma
    Comment with ID: 73595
    Markus posted this reply April 12, 2011 @ 04:29

    And here goes the helper I wanted to avoid:

    @helper RemoveParagraph(HtmlString s)
    {
    @Html.Raw(umbraco.library.RemoveFirstParagraphTag(s.ToString()))
    }

    Ca be called like this:

    @Helpers.RemoveParagraph(Model.myfield)

     


  • Avatar33posts57karma
    Comment with ID: 74491
    Markus posted this reply April 20, 2011 @ 08:55

    Actually, after having encountered some problems, I discovered that the method used for removing the paragraphs Tag in Umbraco:Item (stripParagraph) and the method Umbraco.library.RemoveFirstParagraphTag do not do the same.

    Here's RemoveFirstParagraphTag from library.cs:

            /// <summary>
            /// Removes the starting and ending paragraph tags in a string.
            /// </summary>
            /// <param name="text">The text.</param>
            /// <returns>Returns the string without starting and endning paragraph tags</returns>
            public static string RemoveFirstParagraphTag(string text)
            {
                if (String.IsNullOrEmpty(text))
                    return "";
                text = text.Trim().Replace("\n", string.Empty).Replace("\r", string.Empty);
                if (text.Length > 5)
                {
                    if (text.ToUpper().Substring(0, 3) == "<P>")
                        text = text.Substring(3, text.Length - 3);
                    if (text.ToUpper().Substring(text.Length - 4, 4) == "</P>")
                        text = text.Substring(0, text.Length - 4);
                }
                return text;
            }

    And here's the part of the code found in item.cs which executes when stripParagraph is true:

                // TODO: Needs revision to check if parameter-tags has attributes
                if(helper.FindAttribute(attributes, "stripParagraph") == "true" && _fieldContent.Length > 5)
                {
                    _fieldContent = _fieldContent.Trim();
                    string fieldContentLower = _fieldContent.ToLower();

                        // the field starts with an opening p tag
                    if (fieldContentLower.Substring(0, 3) == "<p>"
                        // it ends with a closing p tag
                        && fieldContentLower.Substring(_fieldContent.Length - 4, 4) == "</p>"
                        // it doesn't contain multiple p-tags
                        && fieldContentLower.IndexOf("<p>", 1) < 0)
                    {
                        _fieldContent = _fieldContent.Substring(3, _fieldContent.Length - 7);
                    }
                }

    RemoveFirstParagraohTag does not only remove the paragraph tags, but also all the linebreaks (not sure why).

    StripParagraph in Item.cs on the other hand only strips the paragraph tag if there are no other paragraphs inside.

    The solution I finally used is a roll-your-own helper function.


  • Avatar209posts354karma
    Comment with ID: 107833
    Douglas Ludlow posted this reply February 13, 2012 @ 08:25

    So...

    @helper StripParagraph(string html)
    {
        // TODO: Needs revision to check if paragraph tag has attributes

        if (html.Length > 5)
        {
            html = html.Trim();
            string htmlLower = html.ToLower();

            // the field starts with an opening p tag
            if (htmlLower.Substring(0, 3) == "<p>"
                // it ends with a closing p tag
                && htmlLower.Substring(html.Length - 4, 4) == "</p>"
                // it doesn't contain multiple p-tags
                && htmlLower.IndexOf("<p>", 1) < 0)
            {
                html = html.Substring(3, html.Length - 7);
            }
            @Html.Raw(html)
        }
    }

  • Avatar796posts608karma
    Comment with ID: 112140
    Greg posted this reply March 25, 2012 @ 03:49

    This is great, but what if you want to strip the tags from a page element called directly from a template?


  • Avatar209posts354karma
    Comment with ID: 112284
    Douglas Ludlow posted this reply March 26, 2012 @ 06:16

    You mean like so:

    <umbraco:Item field="myfield" stripParagraph="true" runat="server" />

  • Tatiana Vassilieva posted this reply February 15, 2013 @ 02:42

    Okay, I don't know what version this is refering to but I'm with Umbraco 4.9 and it is using tinyMCE as the text field datatype;
    (if using that) why not just change the tinyMCE config to strip empty paragraph tags, or remove them, or force br for newline instead?


Pages:

Please login or Sign up To post replies