按条件压缩js

Compress js on condition

我有这个代码:

{% if not variable %}                                                                                                        
{% compress js %}                                                                                                                             
  <script src="{% static "js/init.js" %}"></script>                                                                                             
{% endcompress %}                                                                                                                             
{% endif %}                                                                            

当我启用压缩器时,脚本始终处于活动状态,当变量也为 True 时。

您是否尝试过围绕接受条件作为参数的 compress 模板标签编写包装器?从理论上讲,这个包装器可以组合 ifcompress 标签来为您提供所需的结果。

有关编写您自己的模板标签的更多信息,请参阅 Django 文档的 Writing Custom Template Tags 部分。

此外,请查看 Github 上 django-compressor 源代码中的 compress template tag