David Ziegler's personal blog of computing, math, and other heroic achievements.


06 Jul 2009

Django-css Version 2 is out

I just released version 2 of Django-css, and it’s on github now.

http://github.com/dziegler/django-css/tree/master

This is a significant departure from version 1 because it uses django_compressor rather than django-compress to do compression and versioning. As a result, it’s much easier to use and setup.

Usage is virtually identical to django_compressor, except you can also include HSS, Sass, CleverCSS, etc files in addition to just CSS. For example:

{% compress css xhtml %}
<link rel="stylesheet" href="{{MEDIA_URL}}css/reset.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="{{MEDIA_URL}}css/base.ccss" type="text/css" charset="utf-8" />
{% endcompress %}
will render something like
<link rel="stylesheet" href="/static/CACHE/css/f7c661b7a124.css"
    type="text/css" media="all" charset="utf-8" />
(be sure to use the xhtml argument if you’re using xhtml, otherwise <link> won’t self-close). The only additional piece of work you need is something like this in your settings file:
COMPILER_FORMATS = {
    '.sass': {
        'binary_path':'sass',
        'arguments': '*.sass *.css'
    },
    '.hss': {
        'binary_path':'/home/dziegler/hss',
        'arguments':'*.hss'
    },
    '.ccss': {
        'binary_path':'clevercss',
        'arguments': '*.ccss'
    },
}

to tell django-css where to find your css compiler binaries, and how to pass it arguments.

If you’re not familiar with django_compressor, it’s basically a more elegant django-compress. From the django_compressor readme:

JS/CSS belong in the templates
Every static combiner for django I’ve seen makes you configure your static files in your settings.py. While that works, it doesn’t make sense. Static files are for display. And it’s not even an option if your settings are in completely different repositories and use different deploy processes from the templates that depend on them.
Flexibility
django_compressor doesn’t care if different pages use different combinations of statics. It doesn’t care if you use inline scripts or styles. It doesn’t get in the way.
Automatic regeneration and cache-foreverable generated output
Statics are never stale and browsers can be told to cache the output forever.

So now you include your css/js in templates, where they belong. Also, with django-compress you have to play around with a bunch of settings variables that not even I totally understand to get your versioning to work correctly. With django_compress if you want to update your compressed and versioned css/js, just flush the cache. During development you’re probably using

CACHE_BACKEND = 'dumy:///'
so it should automatically be updated.

Here is my nightmarish settings configuration with django-css version 1:

# django_css
COMPILER_FORMATS = {
    '.ccss': {
        'binary_path': 'clevercss',
        'arguments': 'SOURCE_FILENAME.ccss'
    }
}
COMPRESS_YUI_BINARY = os.path.join('java -jar '+PROJECT_PATH,'yuicompressor-2.4.2.jar')
COMPRESS=False
COMPRESS_CSS = {
    'css': {
        'source_filenames': ('css/reset.css','css/base.ccss','css/thickbox.css',
                             'css/uni-form-generic.css','css/uni-form.css',
                             'css/jquery-ui-1.7.2.custom.css',
                             'css/jquery.rating.css'),
        'output_filename': 'css/all_compressed.r?.css'
    },
    'iefix_css': {
        'source_filenames': ('css/iefix.ccss',),
        'output_filename': 'css/iefix_compressed.r?.css'
    }
}
COMPRESS_JS = {
    'js': {
        'source_filenames': ('js/json2.js','js/jquery.corners.min.js',
                             'js/jquery.form.js','js/thickbox.js','js/uni-form.jquery.js',
                             'js/jquery.MetaData.js','js/jquery.rating.js',
                             'js/jquery-ui-1.7.2.custom.min.js',
                             'js/base.js'),
        'output_filename': 'js/all_compressed.r?.js'
    }
}
COMPRESS_AUTO = True
COMPRESS_VERSION = True 
COMPRESS_CSS_FILTERS = ('django_css.filters.yui.YUICompressorFilter',)
COMPRESS_JS_FILTERS = ('django_css.filters.yui.YUICompressorFilter',)

and with django-css version 2:

# django_css
COMPILER_FORMATS = {
    '.ccss': {
        'binary_path':'clevercss',
        'arguments': '*.ccss'
    },
}
COMPRESS_YUI_BINARY = os.path.join('java -jar '+PROJECT_PATH,'yuicompressor-2.4.2.jar')
COMPRESS_CSS_FILTERS = ('compressor.filters.yui.YUICSSFilter',)
COMPRESS_JS_FILTERS = ('compressor.filters.yui.YUIJSFilter',)

So, I think this is a pretty big improvement. As always, feel free to send me any suggestions, comments, or questions. Plus, now that it’s on github you can fork or push changes.

Comments (View)

blog comments powered by Disqus
Page 1 of 1