Inspired by a resent blog post by Stefan Kip and a conversation with Geert Pasteels on twitter, I thought it was about time I had a play with .LESS and some js/css minification for my sites.
One of the things that did bug me about Stefan’s post though was the use of Squishit for compressing the js/css files, when Umbraco already has a compression framework built-in thanks to the Client Dependency Framework built by the guys over at The Farm. Now, I don’t have anything against Squishit, but I just don’t see the point of including another library, when there is a great solution available straight out of the box.
Now the setup isn’t exactly tricky, but I think sometimes it just takes a little example for people to see how easy it is to kick-start them to give it go, so here is how I did it.
.LESS
So first off, you’ll want to go download the latest version of .LESS from www.dotlesscss.org.
To install, simply drop the DLL’s into your bin folder and setup the HTTP Handler in your web.config as follows:
IIS6 <configuration> <system.web> <httpHandlers> <add verb="*" path="*.less" type="dotless.Core.LessCssHttpHandler, dotless.Core" validate="false" /> </httpHandlers> </system.web> </configuration> IIS7 <configuration> <system.webServer> <handlers> <remove name="DotLessCss" /> <add name="DotLessCss" verb="*" path="*.less" type="dotless.Core.LessCssHttpHandler, dotless.Core" preCondition="integratedMode" /> </handlers> </system.webServer> </configuration>
Client Dependency Framework
To use the Client Dependency Framework, you’ll first need to register a couple of assemblies at the top of your master page as follows:
<%@ Register TagPrefix="umb" Namespace="ClientDependency.Core.Controls" Assembly="ClientDependency.Core" %> <%@ Register TagPrefix="umb" Namespace="umbraco.uicontrols" Assembly="controls" %>
Followed by an UmbracoClientDependencyLoader control at the top of your head tag like so:
<umb:UmbracoClientDependencyLoader runat="server" ID="ClientLoader" />
Putting them together
Once everything is set up, you can then just add a reference to your .less file(s) using the CssInclude control
<umb:CssInclude ID="site" runat="server" FilePath="~/css/site.less" />
And that’s it. I did say it was going to be simple =)
One thing to note is that the Client Dependency Framework will actually make a HTTP request to load the .less file which is a little slower than reading from the file system, but as the result is being cached, I really don’t see this as a problem. On the plus side, because it is using a HTTP request, the Client Dependency Framework has no dependency on the .LESS dll’s, so you are free to update to the latest version as and when they are released.
I’ve been meaning to play with .LESS for a long time, and having started to build production sites in Umbraco 4.5, these are definitely a couple of techniques I’ll be using a lot more of in future.
I hope this post helps to kick-start your switch over =).
umb:
What a lot of people do is path=”*.less.css” instead path=”*.less” that way you get some intellisense in VS. It’s a little buggy but it’s better than nothing.
Good suggestion, but that won’t work with Client Dependency Framework out of the box, as it uses the file extension to work out whether to load from disk or via HTTP. Using the *.css extension would make it load from disk and so wouldn’t get parsed by .LESS.
You can configure the Client Dependensy Framework to always load *.css files via HTTP, but it may give you a bit of a performance hit. Just remove .css from the fileDependencyExtensions attribute in ClientDependency.config, then reconfigure your handlers path to *.less.css. An added benefit to this is it also allows your .less files to show in the Umbraco UI aswell. I’d still like to find a way to get normal css files to load from disk though.
Awesome post! Any idea how to make CD play nicely with http://validator.w3.org/ Get errors with ampersands.
Nice post. dotlesscss.com doesn’t work at the moment – take a look at http://www.tigraine.at/2010/08/17/www-dotlesscss-com-is-down/
They had some hosting problems, try:
http://www.dotlesscss.org
Still alive and kicking.
Thanks Bob,
I’ve updated the link in the post.
Has anyone got this working with a .less.css extension?
I end up with a blank CSS file
Cheers
Hey Ben,
I use .less.css all the time. You should just need to change the path for the HTTPHandler to *.less.css (I don’t tend to use client dependency on the front end, so I don’t worry about the client dependency steps)
Have you made sure you’ve changed the right path attribute? (ie both the IIS6 and IIS7 definitions)
Hi Matt
Thanks. It was using client dependency with the .less.CSS that I was having problems with. So I only ended up using it for .ja as suggested
Cheers
Ben
Hey Matt,
Great post, many thanks.
I’ve been using .less for a while and found it super useful.
I’m a little confused though, as you now say you don’t use client dependency on the front end. If so how are using it, if at all? (you seem to be using it in your original post)
My main concern is that the CSS files are getting ‘built’ on each load, not sure if .less deals with this side automatically?
Appreciate your thoughts.
Cheers
Rich
Hi Rich,
Personally, I’m not using Client Dependency on the front end of my sites, this post was more a “this is how you could do it” as an alternative to Stefan’s post. .less does have some caching built in, so will create a dependency on the .less file. If that file changes, then it’ll update it’s cache. In this case, the only benefit to using Client Dependency would be to merge multiple files and/or compress file contents, which for me, isn’t really an issue ATM.
Cheers
Matt
Hi Matt
Nice blogpost – I’m just getting started with LESS and I’ve read this post to get me started.
However it seems to me that dotless provides one the option to decide if the stylesheet should be cached and minified or not?
Right now I’m not sure I get why I should use clientdependency since it seems to me that caching and minifying comes out of the box?
Am I missing something obvious?
I wasn’t aware you could do that. Let me know how you get on, i’d be interested to now if you can.
Matt
Sweet! Nice find, and thanks for posting the link.
Matt