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! :)
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.
@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) } }
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?
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.
Remove paragraph tags with razor
Hi,
With "classic" umbraco, I can strip the surrounding paragraph tags
Can I do that with razor, too, when calling @Model.myfield ?
Not tested
@umbraco.library:RemoveFirstParagraphTag(Model.myfield)
Rich
That's unfortunately not possible. You could try to do something like:
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.
The following based on Rich's (accepted) answer works:
Of course the elegance is gone ... :-(
Thanks!
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! :)
And here goes the helper I wanted to avoid:
Ca be called like this:
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:
And here's the part of the code found in item.cs which executes when stripParagraph is true:
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.
So...
This is great, but what if you want to strip the tags from a page element called directly from a template?
You mean like so:
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?
Is it the first paragraph or all <p> tags
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>
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.