Frage

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!

War es hilfreich?

Lösung

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

Andere Tipps

You need to wrap them in quotation marks. Try var multilinestring = ''{{ body|safe}}";

The best thing you can do to JSON encode the string.

That will take care of any new lines as well as other special characters.

I don't know the server-side stuff to encode, but on the client it would look something like:

var multilinestring = JSON.parse(jsonEncodeStringHere);

JavaScript strings cannot span multiple lines without a trailing backslash. That is the token error

Try:

var multilinestring = "{{body|safe|replace('\n', '\')}}";

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top