Developer forum

Forum » Development » Limiting text CKEditor text length

Limiting text CKEditor text length

Casper Andersen
Reply

Hi, basically what i want, i for the content editor of our website to be able to write some text in a rich text editor, and then i want to be able to cut it at around 200 words and put a read more link that then does something, but when i try and us as an example the substr method in C# on the value comming from the CKEditor, it messes up the page, i'm thinking this is because im cutting away some html tags and that is what messes up the page, anyone have any ideas on a fix / solution for this, also need to use it on the news section of our website


Replies

 
Nicolai Høeg Pedersen
Reply

Hi Casper

That is not that simple - you would need to make sure that all open html tags are closed up again after you have found the 200 words.

I see 4 options

  • You can strip the HTML from the text first, and then take the first 200 words (substring of the index of the 200th space).
    If in html templates you can use tag extensions: http://templates.dynamicweb.com/TemplateTags/Dynamicweb-template-tags/General-tags/Tag-extensions.aspx
    I.e. <!--whateverTag.StripHtml().Remove(0, 1000) (show the first 1000 characters of that tag)
  • If you want to keep the markup, you need to do somecoding I think. You can use HtmlAgilityPack to load the markup from the editor, run through all text nodes and strip words to 200 and output the html of the HtmlDoc instance.
  • In the template print out the entire content from the editor, all of it - and then
    • Using CSS limit the height and width of the containing html element (div) using overflow hidden, and add a read more button
    • Using JS, count words using the DOM inside the containing html element and remove words after the first 200

Hope this helps.

BR Nicolai

 
Nicolai Høeg Pedersen
Reply

The css hack is probably the best one...

 

You must be logged in to post in the forum