Copied to clipboard

Flag this post as spam?

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


  • Henrik Hammarström 58 posts 54 karma points
    Jan 19, 2010 @ 08:12
    Henrik Hammarström
    0

    News teaser ??

    Hi
    How can I do so that I only see the first 50 characters on a news on the first page and then have to click on "Read more" to get to the news page

     

    So I want to count characters in a string ?

     

    /Henrik

  • Finn 86 posts 50 karma points
    Jan 19, 2010 @ 08:39
    Finn
    2

    Hi Henrik

    This will do want you want:

           <a href="{umbraco.library:NiceUrl(@id)}">
    <xsl:value-of select="substring(umbraco.library:StripHtml(data [@alias = 'bodyText']),1,50)" disable-output-escaping="yes"/>
    </a>

    Change the 'bodyText' to your property.

    /Finn

  • Kim Andersen 1447 posts 2196 karma points MVP
    Jan 20, 2010 @ 23:47
    Kim Andersen
    0

    I usually use the umbraco library-function, truncate string. By using this function it is possible to insert a string after the text, if the text is longer than the max number. You can use it like this:

    <xsl:variable name="text" select="./data[@alias='content']" />
    <xsl:value-of
    select="umbraco.library:TruncateString($text,50,'...')"/>

    You dont have to put the xpath/selection into a variable, but this is just an example. But Finn's example could work as well. I just prefer the Truncate String because of the three dots that wil be inserted after the, only if the text is longer that the max. This gives a good flexibility. Your choice :)

     

    /Kim A

  • Paul Blair 466 posts 731 karma points
    Jan 21, 2010 @ 01:03
    Paul Blair
    0

    Kim, the danger with your solution is that if it is HTML content the string may be truncated half way through an anchor tag (or some other HTML element) and really stuff up the formatting.

    You could have the stripHTML function in your variable for safety.

  • Kim Andersen 1447 posts 2196 karma points MVP
    Jan 21, 2010 @ 22:50
    Kim Andersen
    0

    Hi Paul

    I use the stripHtml-function as well together with my truncate string-function. Forgot to mention it in my earlier post.

    But you are totally right Paul. Another good thing about the stripHtml is to make a newslist consistent. So you don't end up with some teasers being bold, some being italic and so on.

     

    /Kim A

  • Paul Blair 466 posts 731 karma points
    Jan 22, 2010 @ 03:00
    Paul Blair
    0

    I also played around with a function once to try and determine if the truncate was happening within a HTML tag (other than <p></p>).

    It was quickly getting out of hand though so we changed it to previewing 2 paragraphs instead...much easier. Here is the code:

    public string getEndPoint(string sHTML, int length)
    {
      int pos=0;
      for (int i=1;i<=length;i++)
      {
        pos = sHTML.IndexOf("</p>",pos+1);
        if (pos == -1)
          break;
      }
      if (pos >= 0)
        return sHTML.Substring(0, pos) + "</p>";
      else
        return sHTML;
    }

    Some other possible solutions that I can think of are:

     - have another property on the page for the editor to populate with the teaser text with. This gives the best result but requires some double data entry by the user.

     - would it be possible to introduce a new style in the editor (e.g. teaser text) that creates a span around the text you want to display? You can then use a function to strip out the HTML within the span

Please Sign in or register to post replies

Write your reply to:

Draft