user2200321 user2200321 - 3 months ago 30
Javascript Question

Multiline strings in Javascript

I have a "multiline" string in my database that looks something like this:

This
is
a
multiline
string


I'm using Google's "Datstore" so it looks exactly like this in the viewer. There are no
\n
's or things like that.

The problem is that when I try to set the retrieved string to a Javascript variable, I get an "ILLEGAL TOKEN" error. I'm setting it via Jinja2 so it looks like this:

<script>
var multilinestring = {{body|safe}}
</script>


I set other variables like this as well, but they are all integers or single line strings. Any help would be awesome!

Answer

If it's a multi line string, than you must have a new line character at the end of each line (e.g \n)

You can use the replace filter and see if you can remove the new line character e.g.

var multilinestring = {{body|safe|replace("\n", " ")}} // not tested