I am returning a DTML Template generated using Python's Zope and assigning it a JS variable by evaluating it in browser. My template would look like:
var html = <dtml-var "html">
# which is nothing but
var html = "<html>
<body>
Hello
</body>
</html>
"
This is not valid since Javascript expects double quotes to close before a newline character as explained in some questions like JavaScript string with new line - but not using \n.
I figured that I could actually do:
var html = String.raw`<dtml-var "html">`
which works in Chrome, however String.raw
doesn't exist in IE 11
.
Are there any alternatives that I can use?
Alternatively, can I fix this problem in Python by doing the below snippet? Are there any caveats to be aware of?
# Replace newline character to \n literally.
html.replace("\n",\\n")
# Escape the double quotes
html.replace("\n",\\n").replace("\"", "\\\"") i.e replace " with \"