Total Pageviews

Sunday, 1 June 2014

django-farstyle

A django tool for optimized serving of css and javascript assets.

Farstyle simplifies the management and serving of CSS and JavaScript asset files. Required css or js files, conveniently defined and ordered in the database, are merged and compressed, then inserted into your HTML using a single templatetag. Unique file name used for merged asset files enable far-future caching, with a change in any file causing a new merged asset file to be generated and loaded by browsers.
The merging and optimization process is fully customizable, offering control over both the overall processing of merged assets, as well as the handling of each individual file. CSS and JavaScript files can be individualy exempted from merging or optimization, and custom conditions can be used to group and serve files as separate assets based on request parameters or optional templatetag variables.
Farstyle also helps with the creation of CSS and JavaScript files, by allowing them to be rendered from Django templates. Farstyle searches for files in both template provider locations and static filesystem directories, creating the opportunity to override static files with same-name templates.
For development, merging and optimization are disabled when DEBUG is set to True. In this case individual tags are created for each file, with an added query string parameter which ensures that changed files will always be reloaded by browsers.
A bonus feature enables far future caching of media files, such as large images that rarely change.

Main Features

  • Define CSS and JavaScript files in the database. Control the files' order, toggle merging/compression for each file, and write custom Python rules to determine when files are to be used.
  • Create CSS and JavaScript files using django templates. Use constants in your template-based CSS, defined in an optional settings module. Override static CSS and JS files with template-based versions (especially useful when combined with django-dbtemplates).
  • Write CSS in less or sass format.
  • Include all defined CSS and JS files in your templates using a single templatetag (single tag for each asset type, with the provision for using multiple tags combined with conditional statements).
  • Create far-future-caching friendly image tags for large images that rarely change.
  • CSS and JS files are merged down to the smallest possible number of asset files to optimize loading, and are named using unique names which allow browsers to cache assets while immediately loading new versions when assets are changed.
  • Merged and optimized asset files are re-generated on demand when any of the original CSS or JS files are modified.
  • The Sites application is supported, using Many-to-Many relationships so that multiple sites can share common assets.

Credits

Farstyle's database-based specification of CSS and JavaScript files was inspired by django-assetpackager.
The use of customizable filter chains to control optimization and compression of assets was inspired by django-compress, and much of the filtering code was taken from that project.
The Python version of jsmin insluded in Farstyle was translated to Python by Baruch Evan, from the original jsmin implementation by Douglas Crockford.

from http://code.google.com/p/django-farstyle/