CSS symfony plugin with LESS

OK, so we were about to reinvent the wheel when Antoine Abt suggested we take a look at lessphp, a very cool project by Leaf Corcoran starting to port LESS into PHP. We bit the bait and it paid off. Leaf was super-cool about us including the lib into the symfony plugin and we released a new and better plugin to use LESS in symfony. Updating the old CSS plugin with the new one using LESSphp made sense on several levels:

1) LESS augments CSS with some great features: variables, nested inheritance, mixins and accessors and other neat stuff.
2) LESS was already a popular Ruby gem, so it’s syntax is well documented. It should be fairly easy for the symfony community to start using it.
3) We are firm believers of “If something already exists and works well, don’t reinvent it.”
4) Leaf had already done most of the hard work. Porting it to symfony was an easy task.

How the plugin works

Download the plugin from http://www.symfony-project.org/plugins/sgLESSPlugin and install according to instructions. Once the plugin is installed, create a file ending with the extension .less in the /web/css folder of your application. In our case we created a file called main.less. What sgLESSPlugin will do is parse main.less and dynamically generate the output when accessed as /css/main.css, so in your view.yml, all you have to do is call:

1.stylesheets:    [/css/main.css]

Using LESS

The 5 main features of LESS are: variables, nested inheritance, mixins, accessors, and operations.

Variables
Variables allow you to define values of specific things like colors, font-family, width etc… in one place and use them elsewhere. The syntax to use variables is:

@pagewidth: 960px;
@purple: #5d2458;

body {
background-color: @purple;
}

.wrapper {
width: @pagewidth;
}

The output for this will be:


body {
background-color: #5d2458;
}

.wrapper {
width: 960px;
}

Nested inheritance

Nested inheritance is my OCD dream come true. Instead of writing out each rule separately I can nest them in a nice neat fashion.


.bottomnavigation {
width: @pagewidth;
a {
color: #000;
:hover {
color: #ffcc00;
}
}
}

Becomes:


.bottomnavigation {
width: 960px;
}

.bottomnavigation a {
color: #000;
}

.bottomnavigation a:hover {
color: #ffcc00;
}

Mixins

Mixins are classes that allow other rules to be embedded in them.


.round_corners {
border-radius: 4px;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;

}

#pricebox {
.rounded_corners;
}

#promobox {
.rounded_corners;
background-color: #fc0000;
}

Becomes:


.round_corners {
border-radius: 4px;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;

}

#pricebox {
border-radius: 4px;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
}

#promobox {
border-radius: 4px;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
background-color: #fc0000;
}

Accessors

Accessors allows you to access values in other classes.


.article {
color: #010101;
}

.comment {
color: .article['color'];
}

Becomes:


.article {
color: #010101;
}

.comment {
color: #010101;
}

Operations
Operations let you add, subtract, divide and multiply property values and allow you to change multiple values proportionally.

@borderwidth: 2px;

.boxBorder {
border: @borderwidth * 3 solid #ccc;
}

Becomes


.boxBorder {
border: 6px solid #ccc;
}

That’s the simple explanation. To explore further, check out the lessPHP homepage or http://lesscss.org/.

Tags: , , ,

30

07 2009

7 Comments Add Yours ↓

The upper is the most recent comment

  1. 1

    Hey,

    You’re fast :)

  2. Glenn McLelland #
    2

    What about Compass? How does it compare with Less?

    http://wiki.github.com/chriseppstein/compass

  3. 3

    Hey,

    Very nice work of both your team and the LESS team!! I wonder though why the plugin is not named sfLESS or something?
    Using all these custom prefixes makes it harder to code, as you always have to remember who made the plugin and what prefix to use. Is it really important to you I think of servergrove when using this plugin? Please serve the community and supply a sfLESS plugin…

    Regards,
    Sjoerd

  4. 4

    Really nice to discover LESS
    That’s the kind of stuff we all need every day.
    Nice to turn it into a plugin, will definitely try it out !

    :)

  5. 5

    @sjoerd

    There has been a lot of discussion about naming plugins, and from what we understand, sf prefix is reserved for official plugins and it is recommended to use the prefix to identify the author of the plugin and avoid conflicts in names. We don’t look for people to remember us when using the plugin, and we feel it does not make things harder. If you look around there are plenty of plugins with special prefixes. thanks for your comment!

  6. 6

    @glenn

    We are not familiar with Compass, but from what it looks like, it is a CSS framework and uses the Saas syntax which is a bit different than LESS. We liked LESS syntax because it is close to standard CSS.

  7. Ian #
    7

    sjoerd – asking them to use the sf prefix for their plugin is like complaining about java developers using their own namespaces for their classes.

    It makes a LOT of sense for each developer to use their own plugin prefix so that they don’t run the risk of clashing plugin names.


1Trackbacks/Pingbacks

  1. ServerGrove Web Hosting Blog » Blog Archive » Tips for configuring CSS in symfony 09 09 09


Your Comment


blog comments powered by Disqus