We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 16183
    • 1,390 Posts
    http://www.usabilitypost.com/2009/06/16/introducing-less-a-better-css/
    http://lesscss.org/

    A version of CSS put together with a compiler called LESS, which stands for Leaner CSS.
    LESS Features

    LESS augments CSS with 4 main features: variables, mixins, nested inheritance and operations.

    Variables let you specify values in one place, like color or widths, and then re-use them throughout the document. So you could for example specify a palette at the top of the file and then re-use it around the stylesheet.

    Mixins are classes that can be embedded in other rules. The syntax is the same as a class (it actually is just a class, “mixin” is just the term used for the feature), and so by referencing that class name in another rule set you can “mix-in” all of the properties of that class to that rule set.

    Nested inheritance is where instead of writing out each rule set separately you nest rules inside one another. This makes the structure of the stylesheet more organized and easier to understand.

    Operations are just that — operations: division, multiplication, addition and subtraction. This helps you set up declarations which may be tied to sizes or colors of other declarations.

    /k
      • 18374
      • 69 Posts
      Used it on my latest project. Loved it and will definitely be using it from here on out. I don’t need the functionality too terribly often but when in the few cases when I do, it’s super handy.

      For whatever reason, I didn’t like SASS too terribly much. No real reason why, just never clicked with me.
        • 16183
        • 1,390 Posts
        Mark,

        How did you find the experience? Did it reduce the development time, etc? I haven’t tried it out yet.

        cheers/k
          • 7231
          • 4,205 Posts
          Sounds interesting...but I prefer to stick with css since that is universal. It seems to me that this would mean that your css file would need to be compiled on the server before being passed to the browser....we could use nested php to do some this (variables for example) and not need to install another compiler on the server or learn a new syntax. If the browser could handle the .less file natively then it would be golden. Unless I missed something.
            [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

            Something is happening here, but you don't know what it is.
            Do you, Mr. Jones? - [bob dylan]
            • 6902
            • 126 Posts
            I fell in love with less... so much cleaner! The problem is getting to use it without completely changing your workflow. Here’s what I did to make it work a little more seamlessly in MODX (using Revo 2.1.2, but should work in any version).


            • 1) install the lessphp compiler (http://leafo.net/lessphp/) into the core directory: "core/components/lesscss/lessc.inc.php"
            • 2) create a snippet for the compiler (I named mine "lessCSS"):

            <?php
            
            // get the contents of the less file
            $string = file_get_contents($modx->config['base_path'].$path.$file);
            
            // run if not empty
            if ($string) {
            	
            	// load the lessphp compiler
            	$core = $modx->config['core_path'];
            	require_once($core.'components/lesscss/lessc.inc.php');
            	
            	// add a variable to get the path to the actual file
            	$string = '@tpl: "'.$modx->config['base_url'].$path.'";'.chr(10).chr(10).$string;
            	
            	// compile and return css
            	$lc = new lessc();
            	return $lc->parse($string);
            }
            



            • 3) create a new modx resource for the css.
              [list]
              [li]name it whatever you want
            • set the content type to CSS
            • set cacheable to off
            • in the content, call the lessCSS snippet (uncached) with the following variables:
              [list]
              [li]&path = the path to the less file relative to the base url
            • &file = the name of the less file
            [/li]
            [/list]
            [/li]
            [/list]

            [[lessCSS? &path=`assets/templates/mytemplate/` &file=`style.less`]]
            



            • 4) Add the link to your css resource to your template and you’re done!:

            <link rel="stylesheet" type="text/css" href="[[~#]]" /> 
            


            Workflow Comments:

            • Caching: I set it up with the snippet and the page as uncached so that it doesn’t recompile every time the page is called. When you are editing the file, just uncheck the cacheable option on the Stylesheet resource—when you’re done, make it cacheable again.
            • [li]Paths: when adding urls to a CSS file, the links are relative to the file itself; since the css that is being generated is technically wherever your CSS resource is in MODX (not where the less file is) this can get a little annoying when it comes to paths. That’s why I separated out the path and file in the snippet; the snippet adds a @tpl variable to the less output (before compiling) that points to the location of the less file. That way you can save a little bit of time by doing something like this: url({@tpl}images/test.jpg); instead of something like this: url(/assets/templates/mytemplate/images/test.jpg); every time you want a url relative to your less files. You could of course rename @tpl (in the snippet) to anything that makes sense to you.

              • 1169
              • 312 Posts
              Have you tried less.js http://lesscss.org/ especially for development work.

              I know extJS are using SASS + Compass
              I think YUI are at present using/looking at less.js

              Same syntax as CSS with all the SASS goodies (I know SASS has both options)
                DEVELOPMENT ENV:- Ubuntu 12.04 | MODx Revolution 2.2.8 | LAMP 2i Apache 2.2.22 | Php 5.3.10 | Mysql 5.5.31 MySQL client version: 5.5.31
                • 39498
                • 169 Posts
                Thanks for the instruction. works fine for me. only cant import less files. e.g. @import 'colors.less'; with variables wont be compiled, @import 'colors.css'; gets imported (of course only true css). the lessphp documentation says it should work. Cant see, if the snippet needs any adjustment. Any idea on that?
                  • 6902
                  • 126 Posts
                  The latest version of lessCSS has been added to the repository so you can download it via Package Manager.

                  ...as for the import, the only thing I can think of that it might be is to make sure you are using the proper URL structure for CSS:

                  @import 'colors.less'

                  @import url(colors.less);
                    • 39498
                    • 169 Posts
                    I think the problem was, that the less file was read, put in a string and then processed by lessphp. Thereby no imports can be reflected. To get the imports also I changed the code:
                    <?php
                    // get the contents of the less file
                    //$string = file_get_contents($modx->config['base_path'].$path.$file);
                     
                    // run if not empty
                    if(file_exists($modx->config['base_path'].$path.$file)) {
                         
                        // load the lessphp compiler
                        $core = $modx->config['core_path'];
                        require_once($core.'components/lesscss/lessc.inc.php');
                         
                        // add a variable to get the path to the actual file
                        //$string = '@tpl: "'.$modx->config['base_url'].$path.'";'.chr(10).chr(10).$string;
                         
                        // compile and return css
                        //$lc = new lessc();
                        //return $lc->parse($string);
                      
                        $less = new lessc($modx->config['base_path'].$path.$file);
                        print $less->parse();
                    }
                      • 6902
                      • 126 Posts
                      I see the problem now. There is a line missing just before the string is parsed:

                      <?php
                      $lc->importDir = $modx->config['base_path'].$path;


                      When parsing less as a string, you have to tell the compiler the relative directory for imports. (I use imports like crazy.)

                      The reasons I chose to parse the less as a string is so that you only have the less files in your file structure (keeps things nice and clean). The output is generated directly into the MODX resource where MODX can handle the caching. It also makes more sense as you can just reference the MODX file directly in your templates.