File: django-sekizai.txt

package info (click to toggle)
python-django-compressor 4.5.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,108 kB
  • sloc: python: 4,900; makefile: 123; javascript: 5
file content (65 lines) | stat: -rw-r--r-- 2,684 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
.. _django-sekizai_support:

django-sekizai Support
======================

Django Compressor comes with support for django-sekizai_ via an extension.
django-sekizai provides the ability to include template code, from within
any block, to a parent block. It is primarily used to include js/css from
included templates to the master template.

It requires django-sekizai to be installed. Refer to the `django-sekizai docs`_
for how to use ``render_block``

Please note that the sekizai integration does not work with :ref:`offline compression <offline_compression>`.
See `this issue`_ for details.


Usage
-----

In templates which either extend base templates or are included by other templates,
use any of these directives to import Stylesheets and JavaScript files:


.. code-block:: django

    {% load static sekizai_tags %}

    {% addtoblock "css" %}<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css" rel="stylesheet" />{% endaddtoblock %}
    {% addtoblock "css" %}<link href="{% static 'app/css/mystyle.css' %}" rel="stylesheet" type="text/css" />{% endaddtoblock %}

    {% addtoblock "js" %}<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>{% endaddtoblock %}
    {% addtoblock "js" %}<script src="{% static 'js/js/myapp.js' %}" type="text/javascript"></script>{% endaddtoblock %}
    {% addtoblock "js" %}<script async="async" defer="defer" src="https://maps.googleapis.com/maps/api/js?key={{ config.apiKey }}&callback=initMap"></script>{% endaddtoblock %}

Note that some files are loaded by the browser directly from a CDN and thus can not be compressed
by django-compressor. Therefore the Sekizai compressor checks whether a file is compressible, and
only if so, concatenates its payload.

.. code-block:: django

    {% load sekizai_tags %}

    <html>
      <head>
      ...
      {% render_block "css" postprocessor "compressor.contrib.sekizai.compress" %}
      </head>

      <body>
      ...
      {% render_block "js" postprocessor "compressor.contrib.sekizai.compress" %}
      </body>
    </html>

In the above example, we render StyleSheets inside the ``<head>`` element and JavaScript files
just before closing the ``</body>`` tag.

Here, we first render some references to CSS and JavaScript files, served from external
sources. Afterwards all local files are concatenated and optionally minified. In the last
step all references to deferred files are rendered.

.. _django-sekizai: https://github.com/ojii/django-sekizai
.. _django-sekizai docs: https://django-sekizai.readthedocs.io/en/latest/
.. _this issue: https://github.com/django-compressor/django-compressor/issues/351