Jinja2 Support

Django Compressor comes with support for Jinja2 via an extension.

Plain Jinja2

In order to use Django Compressor’s Jinja2 extension we would need to pass compressor.contrib.jinja2ext.CompressorExtension into environment:

import jinja2
from compressor.contrib.jinja2ext import CompressorExtension

env = jinja2.environment(extensions=[CompressorExtension])

From now on, you can use same code you’d normally use within Django templates:

from django.conf import settings
template = env.from_string('\n'.join([
    '{% compress css %}',
    '<link rel="stylesheet" href="{{ STATIC_URL }}css/one.css" type="text/css" charset="utf-8">',
    '{% endcompress %}',
template.render({'STATIC_URL': settings.STATIC_URL})

For coffin users

Coffin makes it very easy to include additional Jinja2 extensions as it only requires to add extension to JINJA2_EXTENSIONS at main settings module:


And that’s it - our extension is loaded and ready to be used.