2

I'm trying to display some rich text in a component with a tpl in my extjs application. I get the rich text from my database and it looks like this:

this is some example text \r\n\r\n with two line breaks

At the moment extjs just displays the text but I want it to also make the line breaks. On the server-side I use PHP and the data then gets loaded into extjs via a direct layer.

Are there any possibilities to do this? The best thing would be if you could somehow translate the rich text to HTML.

Here is an example of my tpl:

tpl:[
 '<h2>Some HTML Title</h2>'
 '{RICH_TEXT}'
 '<p>Some more HTML Stuff</p>'
]

Thanks in advance for your help.

EDIT: I also encountered some cases where there is more than just some line breaks for example:

{\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}}
{\colortbl ;\red0\green0\blue0;}
\viewkind4\uc1\pard\cf1\lang2055\b\f0\fs16 text text text\b0 , more text }

How can I handle this? If there is no possibility, how can I get rid of that stuff and just display the clear text, because this is visible in extjs.

Catshinski
  • 53
  • 1
  • 8
  • When it comes to line breaks you can just use CSS `white-space: pre` for that component. – Krzysztof Dec 11 '14 at 12:04
  • @Lolo Thanks for the tip, but now when there is a lot of text in front of the first line break it stretches the whole component and the text is displayed in one line. Whereas before it automatically made a line break when the text reached the end of the component. – Catshinski Dec 11 '14 at 12:34

1 Answers1

1

In JS you can replace the linebreaks with br tags with the following

str = str.replace(/(?:\r\n|\r|\n)/g, '<br />');

Or, you can do this same process on the PHP side before the data is sent using the built in function nl2br: http://php.net/manual/en/function.nl2br.php

Lesley.Oakey
  • 358
  • 1
  • 7
  • Thanks that helps with the line breaks, but I also need to handle other rich text stuff. I updated the question. – Catshinski Dec 11 '14 at 14:45
  • 1
    If you want all the RTF formatting to display on screen there are 3rd party libraries and functions that do this. See similar question http://stackoverflow.com/questions/188545/regular-expression-for-extracting-text-from-an-rtf-string or GIT project https://github.com/lazygyu/RTF-parser. – Lesley.Oakey Dec 11 '14 at 15:02
  • Thanks for your help. I went with a combination of both: For the first version of rtf strings I use the `nl2br` Method and for the second version of rtf strings I use a RTF to HTML Converter class found [here](http://www.phpclasses.org/package/1930-PHP-RTF-to-HTML-converter-with-latin-character-support.html#information) – Catshinski Dec 19 '14 at 13:58