Copied to clipboard

Flag this post as spam?

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


  • Darin See 4 posts 24 karma points
    Sep 03, 2011 @ 09:32
    Darin See
    0

    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

  • Fuji Kusaka 2203 posts 4220 karma points
    Sep 03, 2011 @ 10:11
    Fuji Kusaka
    0

    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

    <xsl:value-of select="umbraco.library:TruncateString(bodyText, 100, '...')" disable-output-escaping="yes"/>
  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Sep 03, 2011 @ 10:25
    Bo Damgaard Mortensen
    0

    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

  • Rich Green 2246 posts 4008 karma points
    Sep 03, 2011 @ 16:37
    Rich Green
    0

    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

  • Darin See 4 posts 24 karma points
    Sep 03, 2011 @ 17:44
    Darin See
    0

    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

  • Rich Green 2246 posts 4008 karma points
    Sep 03, 2011 @ 18:05
    Rich Green
    0

    Agreed always nicer to let the user decide.

    Rich

Please Sign in or register to post replies

Write your reply to:

Draft