tonic
tonic

Reputation: 453

Respecting the "\n" newlines (and special characters) returned from API response in the HTML display

I am accessing data via an API that returns strings in the following format:

Hello this is some text.\n\nHere's some more that includes a phrase in \"quote marks\".\n\nAnd another thing:\n\n* Bullet point.\n* Bullet point.\n* Bullet point.\n* Fin.

This is a sinatra app, and in my view I simply access the variable and display this with something like:

<%= thing.description %>

When it is rendered, none of the newlines are actually respected. It's all shown as a single line. I'm not sure of the right approach to fix that. Is there a way to replace the \ns with <br> tags? Or something else?

Upvotes: 2

Views: 2428

Answers (1)

lanzz
lanzz

Reputation: 43168

You can enclose the string in a <pre> block (usually has the additional effect of using a monospaced font), or you can enclose it in a block of your choice and style it with white-space: pre;.

Upvotes: 7

Related Questions