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:
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, 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.
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.
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
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
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
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:
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
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.
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
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:
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
is working on a reply...