Minify JavaScript code containing Jinja2 expressions with the Closure Compiler

╄→尐↘猪︶ㄣ 提交于 2019-12-13 05:06:18

问题


I pass a JSON-encoded dictionary from Python 3 to Jinja2 template and assign it to a JavaScript variable. My template is as follows

<script>
    var a = {{ json_dict }}; // is rendered as `var a = {"key": "value"};`
</script>

This works as expected, but I'd like to minify JavaScript code containing Jinja2 expressions using Closure Compiler, which currently throws predictable errors like

JSC_PARSE_ERROR: Parse error. '}' expected at line 2 character 9
var a = {{ json_dict }};

What are my options?


回答1:


You wrap it in an eval or equivalent.

a = eval('({{json_dict}})')


来源:https://stackoverflow.com/questions/19451017/minify-javascript-code-containing-jinja2-expressions-with-the-closure-compiler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!