Django中的Django压缩器和模板标签 [英] Django-compressor and template tags in Django
问题描述
最近我尝试了Django-Compressor.太棒了,我太喜欢了,但是我在离线压缩(COMPRESS_OFFLINE = True
)和模板标签方面遇到了一些麻烦.
Recently I have tried Django-Compressor. It's awesome and i like it too much, but i am having some troubles with offline compression (COMPRESS_OFFLINE = True
) and template tags.
我的JS代码中包含{{ django_vars }}
和{% django_tags %}
,我想Google Clousure编译器(我与Django-Compressor一起使用的过滤器)不知道如何识别它们.实际上,我认为Django模板结构(如{% if something %}
)已删除.
I have {{ django_vars }}
and {% django_tags %}
inside my JS code and i guess Google Clousure compiler (the filter what i'm using with Django-Compressor) doesn't know to identify them. In fact, I think Django template structures like {% if something %}
are deleted.
压缩后,如何在我的代码JS中保留所有这些模板var/tag?
How can i do to keep all this template vars/tags in my code JS after compressing?
感谢队友.
修改:
最终,我发现使用COMPRESS_OFFLINE
遇到的所有问题,都是由于错误而发生的.它们与JS代码中的Django语法无关.当您尝试将JS代码压缩到.html文件而不是外部JS文件中时,该错误始终会出现.所有解析器和所有编译器/压缩器都失败了.
Edit:
Finally, i discovered all problems that i got using COMPRESS_OFFLINE
, they all occurred due to a bug. They were not about Django syntax inside of JS code. The bug always appeared when you tried to compress JS code inside of a file .html, not a external JS. It failed with all parsers and all compilers/compressors.
现在,尽管我比django-pipeline更喜欢django-compressor,但我使用的是最后一个.目前,压缩没有问题.
Now, although i like more django-compressor than django-pipeline, i'm using this last one. At this moment, no problem reported with compression.
感谢所有试图帮助我的人.
Thanks to all who tried to help me.
推荐答案
您尝试过吗? Django Compressor的JS编译器开始工作,IMO渲染了 AFTER 模板节点.因此,{{ django_vars }}
或{% django_tags %}
在压缩之前已经被其输出替换为模板中的内容.
Have you tried that? JS compiler of Django Compressor gets into work AFTER template nodes are rendered, IMO. Thus the {{ django_vars }}
or {% django_tags %}
are already replaced by their outputs in templates before being compressed.
如果每个视图/模板更改了{{ django_vars }}
或{% django_tags %}
.不能将其压缩为一段代码.您最好将这些部分从主要的JS代码块移至JS块,然后分别进行压缩.
If {{ django_vars }}
or {% django_tags %}
are changed per-view/template. It can not be compress to one piece of code. You'd better to move those parts from main JS code blocks to JS block and compress it separately.
这篇关于Django中的Django压缩器和模板标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!