Django Compress: New CSS Filter
I added a new filter today for django-compress that allows for arbitrary string find/replace during the compile process on your css groups.
I had a specific use case for the new filter: my css had a lot of root relative urls for background images. I wanted to host my compiled media on a different host than where my images were being hosted from but this tied the two together.
Furthermore, I didn't want to hard code the absolute urls as I want to be able to test the full environment on my laptop, test environments and finally be able to deploy somewhere else in production. I want the process of changing environments to be simple settings file change.
Therefore, my approach was to create a filter that at compile time execute a string find and replace. It's super simple, but useful, at least to me.
I also found what I think were a couple of bugs when you had some groups with external_urls defined. When they were missing source_filenames and output_filenames definitions, exceptions were raised when running the management command to compile the source. I put some simple checks in place to safe guard against these exceptions.
Commentary
Nice!
I had this on my todo to enable url rewriting in css files. :)
I have another one on my todo for django-compress, template tag support so you dont need to use settings.py at all. If you want to you could think about that one aswell :) I know Andreas Pelme would be glad about it.
I also have a templatetag on my mind which will allow super easy google ajax api support so you dont need to remember what urls to put in external_urls. Basicly it will deprecate external_urls.
Why not just use the django template engine to generate your css file using the {% MEDIA_URL %} tag?
Why couldn't you just use relative paths to the background URLs?
Quite a cool process though, I see how it could be useful for other projects. Such as reusable apps with settings that modify the style. Although, they might be better to swap class names around.... hmmm
Andreas
I like the idea of a template tag driven app so as to alleviate the burden of the settings.py management. Can you elaborate more as to how you would see this being implemented?
I also like the google apis template tag.
Richard
Can you elaborate more as to what you mean. I am not following what you are suggesting in the context of what Django-Compress accomplishes.
Dougal Matthews
Relative paths break down if you are hosting your CSS somewhere different than your images (maybe even just somewhere different than your website -- not sure about this).
Anyway, having the option to be explicit about what you want, I feel is a good thing. Do you disagree?
What do you mean by swapping class names aroun?