The goal is to be able to display snippets of larger strings which may or may not contain html. For instance, a given string might contain 1000 words and html. If I use a truncating function to get the first ten words, say:
I'd be pulling a string that, when displayed, could ruin the design of a given page, due do the unclosed <p> and <ul> tags.
Is there a way to extract the first ten words for the purposes of displaying an abstract of a larger article? Right now I am using: <xsl:value-of select="umbraco.library:TruncateString($post/bodyText, number(1250), '...')" disable-output-escaping="yes"/>
It all depends on how you are planning to truncate yout bodyText either by using XSLT or Razor. From what you posted you are using XSLT so you can achieve this by using
Alternatively, I have used a property with either a RTE or TextBox Multiline on the news/article. This way, the end-user/writer are in control of how the teaser-text appears on the site. In XSLT/Razor I simply check if there is any content in the RTE/TextBox and if it's empty, I'm rendering the TruncateString() as a fallback.
Thanks to all for the replies--three different ways to solve the same problem!
I think that I'm going with Bo's suggestion--with an extra property in the document type, I won't have to worry about truncating the string in a way that leaves unclosed tags and all that.
Smart Truncation of Strings Containing HTML in Umbraco
Hi Umbraco Folks,
I'm not sure whether this question belongs in RAZOR or API, but I've posted here as it makes the most sense to me.
I'm wondering if Umbraco offers a smart truncation function, similar to the one offered in Moveable Type (http://ubiquitic.com/software/smarttrim-movable-type-plugin.html).
The goal is to be able to display snippets of larger strings which may or may not contain html. For instance, a given string might contain 1000 words and html. If I use a truncating function to get the first ten words, say:
"<p>Blah blah blah blah blah blah blah blah blah blah"
I'd be pulling a string that, when displayed, could ruin the design of a given page, due do the unclosed <p> and <ul> tags.
Is there a way to extract the first ten words for the purposes of displaying an abstract of a larger article? Right now I am using: <xsl:value-of select="umbraco.library:TruncateString($post/bodyText, number(1250), '...')" disable-output-escaping="yes"/>
Thank you for your help!
Darin
Hi Darin,
It all depends on how you are planning to truncate yout bodyText either by using XSLT or Razor. From what you posted you are using XSLT so you can achieve this by using
Hi Darin,
As Fuji wrote: that's the way to do it.
Alternatively, I have used a property with either a RTE or TextBox Multiline on the news/article. This way, the end-user/writer are in control of how the teaser-text appears on the site. In XSLT/Razor I simply check if there is any content in the RTE/TextBox and if it's empty, I'm rendering the TruncateString() as a fallback.
All the best,
Bo
Hi,
You can also use http://ucomponents.codeplex.com/wikipage?title=Strings&referringTitle=Documentation 'GetFirstWords' to break on a word and not in the middle of a word, a little nicer.
Rich
Thanks to all for the replies--three different ways to solve the same problem!
I think that I'm going with Bo's suggestion--with an extra property in the document type, I won't have to worry about truncating the string in a way that leaves unclosed tags and all that.
Again, thanks to all,
Darin
Agreed always nicer to let the user decide.
Rich
is working on a reply...