Escape strings for JavaScript using Jinja2?

前端 未结 6 2195
清酒与你
清酒与你 2021-02-18 14:10

How do I escape HTML with Jinja2 so that it can be used as a string in JavaScript (jQuery)?

If I were using Django\'s templating system I could write:

$(         


        
6条回答
  •  清酒与你
    2021-02-18 15:08

    Jinja2 has nice filter tojson. If you make json from string, it will generate string enclosed in double quotes "". You can safely use it in javascript. And you don't need put quotes around by yourself.

    $("#mydiv").append({{ html_string|tojson }});
    

提交回复
热议问题