Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django template's multiline string variable

At the begining of launching my app I am getting variables through django templates variables like so:

var my_variable = '{{ variable }}';

But sometimes it takes more than one line and provide a syntax error

var my_variable = "This should be a good variable;
But it isn't";

How to avoid Uncaught SyntaxError: Unexpected token ILLEGAL?

like image 862
krzyhub Avatar asked Dec 13 '25 06:12

krzyhub


1 Answers

You can try this solution

<script>
    {% autoescape off %}
    var my_variable = "{{ variable|escapejs }}"
    {% endautoescape %}
</script>

escapejs

like image 87
Ivan Semochkin Avatar answered Dec 15 '25 19:12

Ivan Semochkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!