evgeniuz
evgeniuz

Reputation: 2769

Wrap relevant tags with {{ compress js }} and {{ compress css }}

Is there a way to wrap automatically all the relevant tags (i. e. script, style, link rel=stylesheet) with django_compressor tags?

Upvotes: 0

Views: 631

Answers (1)

Steve Jalim
Steve Jalim

Reputation: 12195

EDIT: updated in line with Chris Pratt's comment. Thanks Chris.

Make use of template blocks in your base.html to define {% extra_js %} and {% extra_css %} blocks, then put those blocks inside django-compressor blocks. You may also want to have {% extra_js_nocompress %} and {% extra_css_nocompress %} blocks, too, for stuff that explodes inside compressor (eg Twitter Bootstrap)

So, somewhere in your base.html (and ideally with CSS high up and JS low down):

{% compress css%}
   <link rel="stylesheet" href="{{ STATIC_URL }}foo/bar.css">
   <!-- any other global CSS here too -->
{% endcompress %}

{% compress css%}
   {% block extra_css %}{% endblock %}
{% endcompress %}

{% block extra_css_nocompress %}{% endblock %}



{% compress js%}
   <script type="text/javascript" src="{{ STATIC_URL }}js/waa/baa.js"></script>
   <!-- any other global JS here too -->
{% endcompress %}

{% compress js%}
   {% block extra_js %}{% endblock %}
{% endcompress %}

{% block extra_js_nocompress %}{% endblock %}

Then, in your templates, if the template extends base.html, you can shuffle all your in-template CSS and JS into these blocks (while still keeping them in the template that needs them)

Upvotes: 4

Related Questions