Using CleverCSS in Django
A few minutes ago I pushed CleverCSS to the cheeseshop. So far there is no framework support, once again you have to hack up the bridge yourself, but in this post I’ll show a simple way to get CleverCSS running in django and simplify your CSS files.
The preferred way to use CleverCSS is compiling the clevercss files into css, and not doing that dynamically. But because during development this can be annoying, if you have to recompile your files by hand all the time, I suggest using a view that serves those files dynamically during development. In production usage you just overlay the view URL with an apache directive etc. that points to the folder with the static css files in.
Here the view function, you can store it directly in your urls.py because it’s that small. Otherwise move it into a application responsible for static stuff:
import clevercss
from os import path
from django.http import HttpResponse, Http404
def serve_ccss_file(request, filename):
fn = path.join(path.dirname(__file__), path.pardir, 'styles', filename + '.dcss')
if not path.exists(fn):
raise Http404()
f = file(fn):
try:
css = clevercss.convert(f.read().decode('utf-8'))
return HttpResponse(css, mimetype='text/css')
finally:
f.close()
Now you only have to create an URL rule for that:
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'style/([a-zA-Z_]+).css', serve_ccss_file)
)
All you clevercss files now should go to yourproject/styles, as filename.ccss. If you enter production mode you just open your shell, go to that directory and execute clevercss.py *. (That requires that you have symlinked the clevercss.py file into your PATH.)
To test that, just drop a test.ccss file with the following contents in your styles folder and open http://localhost:8080/styles/test.css:
link_color = red
body:
background-color: white.darken(10)
font-family: 'Lucida Sans', Verdana, sans-serif;
a:
color: $link_color
&:hover:
color: $link_color.brighten(10)
&:visited:
color: $link_color.darken(20)
Lucumr Cogitations » Blog Archive » Using CleverCSS in Django…
Lucumr Cogitations » Blog Archive » Using CleverCSS in Django…
Comment by roScripts - Webmaster resources and websites — Monday, September 17th, 2007 @ 7:01 pm[…] Lucumr Cogitations » Blog Archive » Using CleverCSS in Django […]
Comment by jake elliott @ dai5ychain» Blog Archive » Lucumr Cogitations » Blog Archive » Using CleverCSS in Django — Tuesday, September 18th, 2007 @ 5:47 pm[…] Lucumr Cogitations » Blog Archive » Using CleverCSS in Django once again you have to hack up the bridge yourself, but in this post I’ll show a simple way to get CleverCSS running in django and simplify your CSS files. (tags: css django python clevercss) […]
Comment by links for 2007-09-18 « PaxoBlog — Wednesday, September 19th, 2007 @ 1:20 amnice idea, but actually I would really prefer to stick to the CSS syntax and hide the additional functionality (like variables) inside comments. Why? Simply because the syntax you are suggesting here doesnt work with any syntax highlighter, so finding errors is too hard, anbd you need to learn a new syntax (also if it is not too hard).
And sticking to the current style would not be so hard.
my 2 cents
Wolfram
Comment by Wolfram Kriesing — Friday, November 23rd, 2007 @ 9:50 amThat’s what I heard from quite a few people. Converting the indention based parser into a parser that uses braces to group sections wouldn’t be that hard.
Regards,
Comment by Armin Ronacher — Friday, November 23rd, 2007 @ 11:46 pmArmin