Copied to clipboard

Flag this post as spam?

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


  • Markus 57 karma points
    Apr 12, 2011 @ 15:41
    Markus
    0

    Remove paragraph tags with razor

    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 Green3510 karma points
    Apr 12, 2011 @ 15:51
    Rich Green
    0

    Not tested

    @umbraco.library:RemoveFirstParagraphTag(Model.myfield)

     

    Rich

  • Sebastiaan Janssen9139 karma points admin mvp hq
    Apr 12, 2011 @ 15:54
    Sebastiaan Janssen
    0

    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.

  • Markus57 karma points
    Apr 12, 2011 @ 16:11
    Markus
    0

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

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

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

     

    Thanks!

  • Sebastiaan Janssen9139 karma points admin mvp hq
    Apr 12, 2011 @ 16:26
    Sebastiaan Janssen
    0

    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! :)

  • Markus57 karma points
    Apr 12, 2011 @ 16:29
    Markus
    1

    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)

     

  • Markus57 karma points
    Apr 20, 2011 @ 08:55
    Markus
    0

    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.

  • Douglas Ludlow356 karma points
    Feb 13, 2012 @ 20:25
    Douglas Ludlow
    0

    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)
        }
    }
  • Greg613 karma points
    Mar 25, 2012 @ 03:49
    Greg
    0

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

  • Douglas Ludlow356 karma points
    Mar 26, 2012 @ 18:16
    Douglas Ludlow
    0

    You mean like so:

    <umbraco:Item field="myfield" stripParagraph="true" runat="server" />
  • Tatiana Vassilieva98 karma points
    Feb 15, 2013 @ 02:42
    Tatiana Vassilieva
    0

    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?

  • Fuji Kusaka3956 karma points
    Feb 15, 2013 @ 06:27
    Fuji Kusaka
    0

    Is it the first paragraph or all <p> tags

    @Library.StripHtml(Model.myField, "p")
  • Andrew Davis32 karma points
    May 31, 2013 @ 23:05
    Andrew Davis
    0

    We ran into this same problem on one of our projects and solved it with this simple way of doing it. Wrapping the value with an "@Html.Raw()" fixed the issue.

    <section class="links">

        @{

            var Link = Model.Content.Descendants("links");

        

            <ul>

                @foreach (var links in Link)

                {

                    <li data-category="@(links.GetProperty("weblinkCategory").Value)">

                        <a href="@(links.GetProperty("weblinkAddress").Value)">

                            @(links.GetProperty("weblinkTitle").Value)

                            <span>@Html.Raw(links.GetProperty("weblinkDescription").Value)</span>

                        </a>

                    </li>

           }

            </ul>

        }

    </section>

Please Sign in or register to post replies

Write your reply to:

Draft
Our.umbraco.org is the community mothership for Umbraco, the open source asp.net cms. With a friendly forum for all your questions, a comprehensive documentation and a ton of packages from the community.