9-bits
9-bits

Reputation: 10765

Django to javascript template tag

How do you convert a paragraph of text to something that can be assigned to a javascript variable with django template tags?

eg.

var myVar = {{ my_bio }}

where my_bio is some long paragraph of text with new lines, and other characters.

Upvotes: 1

Views: 394

Answers (1)

Thomas Orozco
Thomas Orozco

Reputation: 55199

The escapejs template tag was created for this purpose, you can have a look at the Django documentation for use cases and examples.

Example:

>>> from django.utils.html import escapejs
>>> escapejs('a\nb')
u'a\\u000Ab'

You can, of course, check out the code itself!

Upvotes: 2

Related Questions