5

I am getting "First Line.↵Second Line↵" as a string in an api response.I want to take this and print in html web page like <span>{{apiText}}</span>. where apiText is a javascript variable whose value is "First Line.↵Second Line↵". But the text appears on one line only.How to I convert to a format where it prints a break.

Bhuwan
  • 177
  • 2
  • 12
kautilya
  • 107
  • 8
  • may be you should share your js code... – Bhuwan Mar 11 '17 at 12:51
  • i am using ember so in the controller i get the text and store in apiText = "First Line.↵Second Line↵" and then in html i just refer apiText in span – kautilya Mar 11 '17 at 12:52
  • Possible duplicate of http://stackoverflow.com/questions/19473021/insert-html-into-view-using-emberjs – Bhuwan Mar 11 '17 at 13:12
  • 1
    Use `{{{` with extremely with cautious, that might lead to security risk. refer https://guides.emberjs.com/v2.11.0/templates/writing-helpers/#toc_escaping-html-content – Ember Freak Mar 11 '17 at 15:05
  • You *could* wrap it in a `
    `.
    – Lux Mar 12 '17 at 14:26

2 Answers2

2

That cr character is \n.

You can replace those with <br> tag.

refinedText: Ember.computed('apiText', function(){
  return this.get('apiText').replace('\n', '<br>');
})

and in template :

{{{refinedText}}
Ebrahim Pasbani
  • 9,168
  • 2
  • 23
  • 30
1

When you are appending the variable to the span . Append it as innerHTML instead of innerText

If you are using Ember.js try using 3 curly braces: <span>{{{apiText}}}</span>

Bhuwan
  • 177
  • 2
  • 12