T4CSS: A T4 Template for .Less CSS With Compression

Good news! I have contributed my T4 template to the .less project. You can access it from the big download button on their homepage.

Pain is often a great motivator for invention, unless you become dull to the pain. I think CSS is one of those cases where there’s a lot of pain that we as web developers often take in stride.

Fortunately not everyone accepts that pain and efforts such as LESS are born. As the home page states:

LESS extends CSS with: variables, mixins, operations and nested rules.

Best of all, LESS uses existing CSS syntax. This means you can rename your current .css files .less and they’ll just work.

LESS solves a lot of the pain of duplication when writing CSS. Originally written as a Ruby gem, Christopher Owen, Erik van Brakel and Daniel Hoelbing ported a version to .NET not surprisingly called .less. Here are some examples from the .less homepage:

.Less implements LESS as an HttpHandler you add to your website mapped to the .less extension. I think this is a great approach when combined with the proper cache headers in the response.

However, sometimes I just want to have static CSS files. So I decided to write a T4 template for .less. Simply drop it in a folder that contains .less files and it will generate a .css file for each .less file.

Not only that, I also added a setting to then minimize the CSS using the YUI Compressor for .NET. This allows you to write your CSS using clear readable .LESS syntax, but deploy minified CSS. To turn off minification just edit the T4 file to change the value of the _minified variable.

Usage is very easy. The following screenshot shows a folder containing a .less file, one for each sample on the .LESS homepage. Notice that I don’t actually reference the YUI compression nor .LESS libraries. I just added them to a solution folder so you could see them (and so the T4 template can find them).

solution-without-t4

With this in place, I can simply drop my T4CSS.tt file into the Css directory and voila!

solution-with-t4

You can see the converted CSS files by expanding the T4CSS.tt node in the solution explorer as shown in the screenshot above.

In order to produce multiple output files, I used the approach that Damien Guard did in his post, Multiple outputs from T4 made easy – revisited. Basically I just embedded his T4 in mine.

You’ll notice there’s an extra file, T4CSS.log, that’s generated. Unfortunately using Damien’s approach, it’s not possible to suppress the default file generated by the T4 template, so I used that to log the operations.

Also note that at the time of this writing, there’s a bug in .LESS that causes one of the samples to throw an exception. I reported it to the developers so hopefully that gets fixed soon.

Lastly, there have been T4 changes in Visual Studio 2010. I didn’t try this template with VS10 yet. When importing the assemblies, you may need to either add them to the GAC or reference them with a full path. When I get some time I’ll play around with that to see how to get it to work.

I have contributed this T4 template to the .less project. You can access it from the big download button on their homepage.

Tags: , , , ,

What others have said

Requesting Gravatar... kelly Dec 02, 2009 2:04 PM
# re: T4CSS: A T4 Template for .Less CSS With Compression
hooooooooooooooorayyyyyyyyyyyy
Requesting Gravatar... Chris Owen Dec 02, 2009 2:12 PM
# re: T4CSS: A T4 Template for .Less CSS With Compression
Hi Phil,

Great work and I'm glad you're enjoying using .Less! We're still beta so there's a few kinks to iron out, but this is a great addition to the project and I'll surely be linking it from our site.

Chris
Requesting Gravatar... Damien Guard Dec 02, 2009 2:14 PM
# re: T4CSS: A T4 Template for .Less CSS With Compression
Nice tool but it's not my approach that prevents the default T4 output - it's the fact T4 is built on Visual Studio's singlefilegenerator :p

[)amien
Requesting Gravatar... Craig Stuntz Dec 02, 2009 2:46 PM
# re: T4CSS: A T4 Template for .Less CSS With Compression
That's excellent! Thanks!
Requesting Gravatar... Chris Owen Dec 02, 2009 2:50 PM
# re: T4CSS: A T4 Template for .Less CSS With Compression
Its probably also worth noting that dotless.Core.dll has a minifier implementation in there so there's no real need for the yahoo YUI compressor.
Requesting Gravatar... Dan F Dec 02, 2009 6:59 PM
# re: T4CSS: A T4 Template for .Less CSS With Compression
You rock Phil! This looks to be awesome. Except, I'm having trouble getting it going in my project. It doesn't seem to matter where I put the dotless.core.dll, I keep getting "Compiling transformation: Metadata file 'dotless.Core.dll' could not be found". I've included it in a solution folder like your screenshot above, and your sample solution doesn't give me the same problem, so I'm kinda out of ideas. Anybody got any tips for me?
Requesting Gravatar... Haacked Dec 02, 2009 7:14 PM
# re: T4CSS: A T4 Template for .Less CSS With Compression
@Dan, copy the DLL into the root of your solution and *then* add it to the Solution folder. It can't just be anywhere on your drive. I think it has to be at the same level as the .sln file.

The Solution Items folder is a "virtual" folder. It doesn't represent where the actual assembly is on disk.
Requesting Gravatar... Dan F Dec 02, 2009 8:35 PM
# re: T4CSS: A T4 Template for .Less CSS With Compression
@Phil, Actually, I rescind part of my previous statement. I'm having the same problem with your sample project too. It didn't show up straight away when I opened the project, but when I right click the T4CSS.tt and choose run custom tool, it gives the same error as above. Which is jolly odd! I shall dig some more :-)
Requesting Gravatar... Dan F Dec 02, 2009 8:57 PM
# re: T4CSS: A T4 Template for .Less CSS With Compression
Ah, Oleg has some handy words about understanding the assembly directive.

If I put an absolute path in to dotless, or add a folder to the project reference paths, it goes OK. I haven't tried the GAC alternative.

All's good again :-)
Requesting Gravatar... Morgan Dec 14, 2009 8:03 AM
# re: T4CSS: A T4 Template for .Less CSS With Compression
Nice! Thank you.
If you want to keep CSS Syntax highlighting (and autoformatting) in VS for now (.less is planning a syntax highglight extension for VS), you can:
-name original files .less.css
-mod .tt line 34 to search for *.less.css
-mod .tt line 266: return Path.GetFileNameWithoutExtension(dotLessFileName.Replace(".less","")) + ".css";
Requesting Gravatar... Arnis L. Dec 28, 2009 7:59 PM
# re: T4CSS: A T4 Template for .Less CSS With Compression
And I thought that I will be first that provides solution for syntax highlighting.

At least i can add that it's a good idea to turn off css error detection in vs settings. :D
Requesting Gravatar... marius Dec 29, 2009 6:28 PM
# re: T4CSS: A T4 Template for .Less CSS With Compression
wow! less looks like fun...
but, why oh why not crunch all css toghether an have just one file uploading in a single request;)

hehe i like damiens manager class so much!

althought oleg has some serious t4 templates too.

Requesting Gravatar... Wayne Brantley Dec 31, 2009 10:32 AM
# re: T4CSS: A T4 Template for .Less CSS With Compression
I have created a real less add-in, no template necessary, automatic running and combining!

blog.waynebrantley.com/...
Requesting Gravatar... Mike Feb 07, 2010 3:01 PM
# re: T4CSS: A T4 Template for .Less CSS With Compression
Hopefully one day this stuff will be as easy as:

gem install less

Right now, it's quite a lot of work to get these kind of cool add-ons/plugins/gems to work in a typical ASP.NET workflow.
Requesting Gravatar... Jon Canning Apr 22, 2010 9:25 AM
# re: T4CSS: A T4 Template for .Less CSS With Compression
$(SolutionDir) is your friend when importing assemblies
Requesting Gravatar... Francis Jun 01, 2010 4:52 PM
# re: T4CSS: A T4 Template for .Less CSS With Compression
Watch chirpy (http://chirpy.codeplex.com/).
Add-in for vs2010 and vs2008 generate on save .css file regular and minified.

What do you have to say?

(will show your gravatar)
Please add 2 and 7 and type the answer here: