Warning, /webapps/ocs-webserver/library/lessphp/docs/docs.md is written in an unsupported language. File is not indexed.
0001 title: v0.4.0 documentation 0002 link_to_home: true 0003 -- 0004 0005 <h2 skip="true">Documentation v0.4.0</h2> 0006 0007 <div style="margin-bottom: 1em;">$index</div> 0008 0009 **lessphp** is a compiler that generates CSS from a superset language which 0010 adds a collection of convenient features often seen in other languages. All CSS 0011 is compatible with LESS, so you can start using new features with your existing CSS. 0012 0013 It is designed to be compatible with [less.js](http://lesscss.org), and suitable 0014 as a drop in replacement for PHP projects. 0015 0016 ## Getting Started 0017 0018 The homepage for **lessphp** can be found at [http://leafo.net/lessphp/][1]. 0019 0020 You can follow development at the project's [GitHub][2]. 0021 0022 Including **lessphp** in your project is as simple as dropping the single 0023 include file into your code base and running the appropriate compile method as 0024 described in the [PHP Interface](#php_interface). 0025 0026 [1]: http://leafo.net/lessphp "lessphp homepage" 0027 [2]: https://github.com/leafo/lessphp "lessphp GitHub page" 0028 0029 ## Installation 0030 0031 **lessphp** is distributed entirely in a single stand-alone file. Download the 0032 latest version from either [the homepage][1] or [GitHub][2]. 0033 0034 Development versions can also be downloading from GitHub. 0035 0036 Place `lessphp.inc.php` in a location available to your PHP scripts, and 0037 include it. That's it! you're ready to begin. 0038 0039 ## The Language 0040 0041 **lessphp** is very easy to learn because it generally functions how you would 0042 expect it to. If you feel something is challenging or missing, feel free to 0043 open an issue on the [bug tracker](https://github.com/leafo/lessphp/issues). 0044 0045 It is also easy to learn because any standards-compliant CSS code is valid LESS 0046 code. You are free to gradually enhance your existing CSS code base with LESS 0047 features without having to worry about rewriting anything. 0048 0049 The following is a description of the new languages features provided by LESS. 0050 0051 ### Line Comments 0052 0053 Simple but very useful; line comments are started with `//`: 0054 0055 ```less 0056 // this is a comment 0057 body { 0058 color: red; // as is this 0059 /* block comments still work also */ 0060 } 0061 ``` 0062 0063 ### Variables 0064 0065 Variables are identified with a name that starts with `@`. To declare a 0066 variable, you create an appropriately named CSS property and assign it a value: 0067 0068 ```less 0069 @family: "verdana"; 0070 @color: red; 0071 body { 0072 @mycolor: red; 0073 font-family: @family; 0074 color: @color; 0075 border-bottom: 1px solid @color; 0076 } 0077 ``` 0078 0079 Variable declarations will not appear in the output. Variables can be declared 0080 in the outer most scope of the file, or anywhere else a CSS property may 0081 appear. They can hold any CSS property value. 0082 0083 Variables are only visible for use from their current scope, or any enclosed 0084 scopes. 0085 0086 If you have a string or keyword in a variable, you can reference another 0087 variable by that name by repeating the `@`: 0088 0089 ```less 0090 @value: 20px; 0091 @value_name: "value"; 0092 0093 width: @@value_name; 0094 ``` 0095 0096 ### Expressions 0097 0098 Expressions let you combine values and variables in meaningful ways. For 0099 example you can add to a color to make it a different shade. Or divide up the 0100 width of your layout logically. You can even concatenate strings. 0101 0102 Use the mathematical operators to evaluate an expression: 0103 0104 ```less 0105 @width: 960px; 0106 .nav { 0107 width: @width / 3; 0108 color: #001 + #abc; 0109 } 0110 .body { 0111 width: 2 * @width / 3; 0112 font-family: "hel" + "vetica"; 0113 } 0114 ``` 0115 0116 Parentheses can be used to control the order of evaluation. They can also be 0117 used to force an evaluation for cases where CSS's syntax makes the expression 0118 ambiguous. 0119 0120 The following property will produce two numbers, instead of doing the 0121 subtraction: 0122 0123 ```less 0124 margin: 10px -5px; 0125 ``` 0126 0127 To force the subtraction: 0128 0129 ```less 0130 margin: (10px -5px); 0131 ``` 0132 0133 It is also safe to surround mathematical operators by spaces to ensure that 0134 they are evaluated: 0135 0136 ```less 0137 margin: 10px - 5px; 0138 ``` 0139 0140 Division has a special quirk. There are certain CSS properties that use the `/` 0141 operator as part of their value's syntax. Namely, the [font][4] shorthand and 0142 [border-radius][3]. 0143 0144 [3]: https://developer.mozilla.org/en/CSS/border-radius 0145 [4]: https://developer.mozilla.org/en/CSS/font 0146 0147 0148 Thus, **lessphp** will ignore any division in these properties unless it is 0149 wrapped in parentheses. For example, no division will take place here: 0150 0151 ```less 0152 .font { 0153 font: 20px/80px "Times New Roman"; 0154 } 0155 ``` 0156 0157 In order to force division we must wrap the expression in parentheses: 0158 0159 ```less 0160 .font { 0161 font: (20px/80px) "Times New Roman"; 0162 } 0163 ``` 0164 0165 If you want to write a literal `/` expression without dividing in another 0166 property (or a variable), you can use [string unquoting](#string_unquoting): 0167 0168 ```less 0169 .var { 0170 @size: ~"20px/80px"; 0171 font: @size sans-serif; 0172 } 0173 ``` 0174 0175 ### Nested Blocks 0176 0177 By nesting blocks we can build up a chain of CSS selectors through scope 0178 instead of repeating them. In addition to reducing repetition, this also helps 0179 logically organize the structure of our CSS. 0180 0181 ```less 0182 ol.list { 0183 li.special { 0184 border: 1px solid red; 0185 } 0186 0187 li.plain { 0188 font-weight: bold; 0189 } 0190 } 0191 ``` 0192 0193 0194 This will produce two blocks, a `ol.list li.special` and `ol.list li.plain`. 0195 0196 Blocks can be nested as deep as required in order to build a hierarchy of 0197 relationships. 0198 0199 The `&` operator can be used in a selector to represent its parent's selector. 0200 If the `&` operator is used, then the default action of appending the parent to 0201 the front of the child selector separated by space is not performed. 0202 0203 ```less 0204 b { 0205 a & { 0206 color: red; 0207 } 0208 0209 // the following have the same effect 0210 0211 & i { 0212 color: blue; 0213 } 0214 0215 i { 0216 color: blue; 0217 } 0218 } 0219 ``` 0220 0221 0222 Because the `&` operator respects the whitespace around it, we can use it to 0223 control how the child blocks are joined. Consider the differences between the 0224 following: 0225 0226 ```less 0227 div { 0228 .child-class { color: purple; } 0229 0230 &.isa-class { color: green; } 0231 0232 #child-id { height: 200px; } 0233 0234 &#div-id { height: 400px; } 0235 0236 &:hover { color: red; } 0237 0238 :link { color: blue; } 0239 } 0240 ``` 0241 0242 The `&` operator also works with [mixins](#mixins), which produces interesting results: 0243 0244 ```less 0245 .within_box_style() { 0246 .box & { 0247 color: blue; 0248 } 0249 } 0250 0251 #menu { 0252 .within_box_style; 0253 } 0254 ``` 0255 0256 ### Mixins 0257 0258 Any block can be mixed in just by naming it: 0259 0260 ```less 0261 .mymixin { 0262 color: blue; 0263 border: 1px solid red; 0264 0265 .special { 0266 font-weight: bold; 0267 } 0268 } 0269 0270 0271 h1 { 0272 font-size: 200px; 0273 .mixin; 0274 } 0275 ``` 0276 0277 All properties and child blocks are mixed in. 0278 0279 Mixins can be made parametric, meaning they can take arguments, in order to 0280 enhance their utility. A parametric mixin all by itself is not outputted when 0281 compiled. Its properties will only appear when mixed into another block. 0282 0283 The canonical example is to create a rounded corners mixin that works across 0284 browsers: 0285 0286 ```less 0287 .rounded-corners(@radius: 5px) { 0288 border-radius: @radius; 0289 -webkit-border-radius: @radius; 0290 -moz-border-radius: @radius; 0291 } 0292 0293 .header { 0294 .rounded-corners(); 0295 } 0296 0297 .info { 0298 background: red; 0299 .rounded-corners(14px); 0300 } 0301 ``` 0302 0303 If you have a mixin that doesn't have any arguments, but you don't want it to 0304 show up in the output, give it a blank argument list: 0305 0306 ```less 0307 .secret() { 0308 font-size: 6000px; 0309 } 0310 0311 .div { 0312 .secret; 0313 } 0314 ``` 0315 0316 If the mixin doesn't need any arguments, you can leave off the parentheses when 0317 mixing it in, as seen above. 0318 0319 You can also mixin a block that is nested inside other blocks. You can think of 0320 the outer block as a way of making a scope for your mixins. You just list the 0321 names of the mixins separated by spaces, which describes the path to the mixin 0322 you want to include. Optionally you can separate them by `>`. 0323 0324 ```less 0325 .my_scope { 0326 .some_color { 0327 color: red; 0328 .inner_block { 0329 text-decoration: underline; 0330 } 0331 } 0332 .bold { 0333 font-weight: bold; 0334 color: blue; 0335 } 0336 } 0337 0338 .a_block { 0339 .my_scope .some_color; 0340 .my_scope .some_color .inner_block; 0341 } 0342 0343 .another_block { 0344 // the alternative syntax 0345 .my_scope > .bold; 0346 } 0347 ``` 0348 0349 0350 #### Mixin Arguments 0351 0352 When declaring a mixin you can specify default values for each argument. Any 0353 argument left out will be given the default value specified. Here's the 0354 syntax: 0355 0356 ```less 0357 .mix(@color: red, @height: 20px, @pad: 12px) { 0358 border: 1px solid @color; 0359 height: @height - @pad; 0360 padding: @pad; 0361 } 0362 0363 .default1 { 0364 .mix(); 0365 } 0366 0367 .default2 { 0368 .mix(blue); 0369 } 0370 0371 .default3 { 0372 .mix(blue, 40px, 5px); 0373 } 0374 ``` 0375 0376 Additionally, you can also call a mixin using the argument names, this is 0377 useful if you want to replace a specific argument while having all the others 0378 take the default regardless of what position the argument appears in. The 0379 syntax looks something like this: 0380 0381 0382 ```lessbasic 0383 div { 0384 .my_mixin(@paddding: 4px); // @color and @height get default values 0385 .my_mixin(@paddding: 4px, @height: 50px); // you can specify them in any order 0386 } 0387 ``` 0388 0389 You can also combine the ordered arguments with the named ones: 0390 0391 ```lessbasic 0392 div { 0393 // @color is blue, @padding is 4px, @height is default 0394 .my_mixin(blue, @padding: 4px); 0395 } 0396 ``` 0397 0398 Mixin arguments can be delimited with either a `,` or `;`, but only one can be 0399 active at once. This means that each argument is separated by either `,` or 0400 `;`. By default `,` is the delimiter, in all the above examples we used a `,`. 0401 0402 A problem arises though, sometimes CSS value lists are made up with commas. In 0403 order to be able to pass a comma separated list literal we need to use `;` as 0404 the delimiter. (You don't need to worry about this if your list is stored in a 0405 variable) 0406 0407 If a `;` appears anywhere in the argument list, then it will be used as the 0408 argument delimiter, and all commas we be used as part of the argument values. 0409 0410 Here's a basic example: 0411 0412 ```less 0413 .fancy_mixin(@box_shadow, @color: blue) { 0414 border: 1px solid @color; 0415 box-shadow: @box_shadow; 0416 } 0417 0418 0419 div { 0420 // two arguments passed separated by ; 0421 .fancy_mixin(2px 2px, -2px -2px; red); 0422 } 0423 0424 pre { 0425 // one argument passed, ends in ; 0426 .fancy_mixin(inset 4px 4px, -2px 2px;); 0427 } 0428 0429 ``` 0430 0431 If we only want to pass a single comma separated value we still need to use 0432 `;`, to do this we stick it on the end as demonstrated above. 0433 0434 0435 #### `@arguments` Variable 0436 0437 Within an mixin there is a special variable named `@arguments` that contains 0438 all the arguments passed to the mixin along with any remaining arguments that 0439 have default values. The value of the variable has all the values separated by 0440 spaces. 0441 0442 This useful for quickly assigning all the arguments: 0443 0444 ```less 0445 .box-shadow(@x, @y, @blur, @color) { 0446 box-shadow: @arguments; 0447 -webkit-box-shadow: @arguments; 0448 -moz-box-shadow: @arguments; 0449 } 0450 .menu { 0451 .box-shadow(1px, 1px, 5px, #aaa); 0452 } 0453 ``` 0454 0455 In addition to the arguments passed to the mixin, `@arguments` will also include 0456 remaining default values assigned by the mixin: 0457 0458 0459 ```less 0460 .border-mixin(@width, @style: solid, @color: black) { 0461 border: @arguments; 0462 } 0463 0464 pre { 0465 .border-mixin(4px, dotted); 0466 } 0467 0468 ``` 0469 0470 0471 #### Pattern Matching 0472 0473 When you *mix in* a mixin, all the available mixins of that name in the current 0474 scope are checked to see if they match based on what was passed to the mixin 0475 and how it was declared. 0476 0477 The simplest case is matching by number of arguments. Only the mixins that 0478 match the number of arguments passed in are used. 0479 0480 ```less 0481 .simple() { // matches no arguments 0482 height: 10px; 0483 } 0484 0485 .simple(@a, @b) { // matches two arguments 0486 color: red; 0487 } 0488 0489 .simple(@a) { // matches one argument 0490 color: blue; 0491 } 0492 0493 div { 0494 .simple(10); 0495 } 0496 0497 span { 0498 .simple(10, 20); 0499 } 0500 ``` 0501 0502 Whether an argument has default values is also taken into account when matching 0503 based on number of arguments: 0504 0505 ```less 0506 // matches one or two arguments 0507 .hello(@a, @b: blue) { 0508 height: @a; 0509 color: @b; 0510 } 0511 0512 .hello(@a, @b) { // matches only two 0513 width: @a; 0514 border-color: @b; 0515 } 0516 0517 .hello(@a) { // matches only one 0518 padding: 1em; 0519 } 0520 0521 div { 0522 .hello(10px); 0523 } 0524 0525 pre { 0526 .hello(10px, yellow); 0527 } 0528 ``` 0529 0530 Additionally, a *vararg* value can be used to further control how things are 0531 matched. A mixin's argument list can optionally end in the special argument 0532 named `...`. The `...` may match any number of arguments, including 0. 0533 0534 ```less 0535 // this will match any number of arguments 0536 .first(...) { 0537 color: blue; 0538 } 0539 0540 // matches at least 1 argument 0541 .second(@arg, ...) { 0542 height: 200px + @arg; 0543 } 0544 0545 div { .first("some", "args"); } 0546 pre { .second(10px); } 0547 ``` 0548 0549 If you want to capture the values that get captured by the *vararg* you can 0550 give it a variable name by putting it directly before the `...`. This variable 0551 must be the last argument defined. It's value is just like the special 0552 [`@arguments` variable](#arguments_variable), a space separated list. 0553 0554 0555 ```less 0556 .hello(@first, @rest...) { 0557 color: @first; 0558 text-shadow: @rest; 0559 } 0560 0561 span { 0562 .hello(red, 1px, 1px, 0px, white); 0563 } 0564 0565 ``` 0566 0567 Another way of controlling whether a mixin matches is by specifying a value in 0568 place of an argument name when declaring the mixin: 0569 0570 ```less 0571 .style(old, @size) { 0572 font: @size serif; 0573 } 0574 0575 .style(new, @size) { 0576 font: @size sans-serif; 0577 } 0578 0579 .style(@_, @size) { 0580 letter-spacing: floor(@size / 6px); 0581 } 0582 0583 em { 0584 @switch: old; 0585 .style(@switch, 15px); 0586 } 0587 ``` 0588 0589 Notice that two of the three mixins were matched. The mixin with a matching 0590 first argument, and the generic mixin that matches two arguments. It's common 0591 to use `@_` as the name of a variable we intend to not use. It has no special 0592 meaning to LESS, just to the reader of the code. 0593 0594 #### Guards 0595 0596 Another way of restricting when a mixin is mixed in is by using guards. A guard 0597 is a special expression that is associated with a mixin declaration that is 0598 evaluated during the mixin process. It must evaluate to true before the mixin 0599 can be used. 0600 0601 We use the `when` keyword to begin describing a list of guard expressions. 0602 0603 Here's a simple example: 0604 0605 ```less 0606 .guarded(@arg) when (@arg = hello) { 0607 color: blue; 0608 } 0609 0610 div { 0611 .guarded(hello); // match 0612 } 0613 0614 span { 0615 .guarded(world); // no match 0616 } 0617 ``` 0618 Only the `div`'s mixin will match in this case, because the guard expression 0619 requires that `@arg` is equal to `hello`. 0620 0621 We can include many different guard expressions by separating them by commas. 0622 Only one of them needs to match to trigger the mixin: 0623 0624 ```less 0625 .x(@a, @b) when (@a = hello), (@b = world) { 0626 width: 960px; 0627 } 0628 0629 div { 0630 .x(hello, bar); // match 0631 } 0632 0633 span { 0634 .x(foo, world); // match 0635 } 0636 0637 pre { 0638 .x(foo, bar); // no match 0639 } 0640 ``` 0641 0642 Instead of a comma, we can use `and` keyword to make it so all of the guards 0643 must match in order to trigger the mixin. `and` has higher precedence than the 0644 comma. 0645 0646 ```less 0647 .y(@a, @b) when (@a = hello) and (@b = world) { 0648 height: 600px; 0649 } 0650 0651 div { 0652 .y(hello, world); // match 0653 } 0654 0655 span { 0656 .y(hello, bar); // no match 0657 } 0658 ``` 0659 0660 Commas and `and`s can be mixed and matched. 0661 0662 You can also negate a guard expression by using `not` in from of the parentheses: 0663 0664 ```less 0665 .x(@a) when not (@a = hello) { 0666 color: blue; 0667 } 0668 0669 div { 0670 .x(hello); // no match 0671 } 0672 ``` 0673 0674 The `=` operator is used to check equality between any two values. For numbers 0675 the following comparison operators are also defined: 0676 0677 `<`, `>`, `=<`, `>=` 0678 0679 There is also a collection of predicate functions that can be used to test the 0680 type of a value. 0681 0682 These are `isnumber`, `iscolor`, `iskeyword`, `isstring`, `ispixel`, 0683 `ispercentage` and `isem`. 0684 0685 ```less 0686 .mix(@a) when (ispercentage(@a)) { 0687 height: 500px * @a; 0688 } 0689 .mix(@a) when (ispixel(@a)) { 0690 height: @a; 0691 } 0692 0693 div.a { 0694 .mix(50%); 0695 } 0696 0697 div.a { 0698 .mix(350px); 0699 } 0700 ``` 0701 0702 #### !important 0703 0704 If you want to apply the `!important` suffix to every property when mixing in a 0705 mixin, just append `!important` to the end of the call to the mixin: 0706 0707 ```less 0708 .make_bright { 0709 color: red; 0710 font-weight: bold; 0711 } 0712 0713 .color { 0714 color: green; 0715 } 0716 0717 body { 0718 .make_bright() !important; 0719 .color(); 0720 } 0721 0722 ``` 0723 0724 ### Selector Expressions 0725 0726 Sometimes we want to dynamically generate the selector of a block based on some 0727 variable or expression. We can do this by using *selector expressions*. Selector 0728 expressions are CSS selectors that are evaluated in the current scope before 0729 being written out. 0730 0731 A simple example is a mixin that dynamically creates a selector named after the 0732 mixin's argument: 0733 0734 ```less 0735 .create-selector(@name) { 0736 @{name} { 0737 color: red; 0738 } 0739 } 0740 0741 .create-selector(hello); 0742 .create-selector(world); 0743 ``` 0744 0745 The string interpolation syntax works inside of selectors, letting you insert varaibles. 0746 0747 Here's an interesting example adapted from Twitter Bootstrap. A couple advanced 0748 things are going on. We are using [Guards](#guards) along with a recursive 0749 mixin to work like a loop to generate a series of CSS blocks. 0750 0751 0752 ```less 0753 // create our recursive mixin: 0754 .spanX (@index) when (@index > 0) { 0755 .span@{index} { 0756 width: @index * 100px; 0757 } 0758 .spanX(@index - 1); 0759 } 0760 .spanX (0) {} 0761 0762 // mix it into the global scopee: 0763 .spanX(4); 0764 ``` 0765 0766 ### Import 0767 0768 Multiple LESS files can be compiled into a single CSS file by using the 0769 `@import` statement. Be careful, the LESS import statement shares syntax with 0770 the CSS import statement. If the file being imported ends in a `.less` 0771 extension, or no extension, then it is treated as a LESS import. Otherwise it 0772 is left alone and outputted directly: 0773 0774 ```lessbasic 0775 // my_file.less 0776 .some-mixin(@height) { 0777 height: @height; 0778 } 0779 0780 // main.less 0781 @import "main.less" // will import the file if it can be found 0782 @import "main.css" // will be left alone 0783 0784 body { 0785 .some-mixin(400px); 0786 } 0787 ``` 0788 0789 All of the following lines are valid ways to import the same file: 0790 0791 ```lessbasic 0792 @import "file"; 0793 @import 'file.less'; 0794 @import url("file"); 0795 @import url('file'); 0796 @import url(file); 0797 ``` 0798 0799 When importing, the `importDir` is searched for files. This can be configured, 0800 see [PHP Interface](#php_interface). 0801 0802 A file is only imported once. If you try to include the same file multiple 0803 times all the import statements after the first produce no output. 0804 0805 ### String Interpolation 0806 0807 String interpolation is a convenient way to insert the value of a variable 0808 right into a string literal. Given some variable named `@var_name`, you just 0809 need to write it as `@{var_name}` from within the string to have its value 0810 inserted: 0811 0812 ```less 0813 @symbol: ">"; 0814 h1:before { 0815 content: "@{symbol}: "; 0816 } 0817 0818 h2:before { 0819 content: "@{symbol}@{symbol}: "; 0820 } 0821 ``` 0822 0823 There are two kinds of strings, implicit and explicit strings. Explicit strings 0824 are wrapped by double quotes, `"hello I am a string"`, or single quotes `'I am 0825 another string'`. Implicit strings only appear when using `url()`. The text 0826 between the parentheses is considered a string and thus string interpolation is 0827 possible: 0828 0829 ```less 0830 @path: "files/"; 0831 body { 0832 background: url(@{path}my_background.png); 0833 } 0834 ``` 0835 0836 ### String Format Function 0837 0838 The `%` function can be used to insert values into strings using a *format 0839 string*. It works similar to `printf` seen in other languages. It has the 0840 same purpose as string interpolation above, but gives explicit control over 0841 the output format. 0842 0843 ```less 0844 @symbol: ">"; 0845 h1:before { 0846 content: %("%s: ", @symbol); 0847 } 0848 ``` 0849 0850 The `%` function takes as its first argument the format string, following any 0851 number of addition arguments that are inserted in place of the format 0852 directives. 0853 0854 A format directive starts with a `%` and is followed by a single character that 0855 is either `a`, `d`, or `s`: 0856 0857 ```less 0858 strings: %("%a %d %s %a", hi, 1, 'ok', 'cool'); 0859 ``` 0860 0861 `%a` and `%d` format the value the same way: they compile the argument to its 0862 CSS value and insert it directly. When used with a string, the quotes are 0863 included in the output. This typically isn't what we want, so we have the `%s` 0864 format directive which strips quotes from strings before inserting them. 0865 0866 The `%d` directive functions the same as `%a`, but is typically used for numbers 0867 assuming the output format of numbers might change in the future. 0868 0869 ### String Unquoting 0870 0871 Sometimes you will need to write proprietary CSS syntax that is unable to be 0872 parsed. As a workaround you can place the code into a string and unquote it. 0873 Unquoting is the process of outputting a string without its surrounding quotes. 0874 There are two ways to unquote a string. 0875 0876 The `~` operator in front of a string will unquote that string: 0877 0878 ```less 0879 .class { 0880 // a made up, but problematic vendor specific CSS 0881 filter: ~"Microsoft.AlphaImage(src='image.png')"; 0882 } 0883 ``` 0884 0885 If you are working with other types, such as variables, there is a built in 0886 function that let's you unquote any value. It is called `e`. 0887 0888 ```less 0889 @color: "red"; 0890 .class { 0891 color: e(@color); 0892 } 0893 ``` 0894 0895 ### Built In Functions 0896 0897 **lessphp** has a collection of built in functions: 0898 0899 * `e(str)` -- returns a string without the surrounding quotes. 0900 See [String Unquoting](#string_unquoting) 0901 0902 * `floor(number)` -- returns the floor of a numerical input 0903 * `round(number)` -- returns the rounded value of numerical input 0904 0905 * `lighten(color, percent)` -- lightens `color` by `percent` and returns it 0906 * `darken(color, percent)` -- darkens `color` by `percent` and returns it 0907 0908 * `saturate(color, percent)` -- saturates `color` by `percent` and returns it 0909 * `desaturate(color, percent)` -- desaturates `color` by `percent` and returns it 0910 0911 * `fadein(color, percent)` -- makes `color` less transparent by `percent` and returns it 0912 * `fadeout(color, percent)` -- makes `color` more transparent by `percent` and returns it 0913 0914 * `spin(color, amount)` -- returns a color with `amount` degrees added to hue 0915 0916 * `fade(color, amount)` -- returns a color with the alpha set to `amount` 0917 0918 * `hue(color)` -- returns the hue of `color` 0919 0920 * `saturation(color)` -- returns the saturation of `color` 0921 0922 * `lightness(color)` -- returns the lightness of `color` 0923 0924 * `alpha(color)` -- returns the alpha value of `color` or 1.0 if it doesn't have an alpha 0925 0926 * `percentage(number)` -- converts a floating point number to a percentage, e.g. `0.65` -> `65%` 0927 0928 * `mix(color1, color1, percent)` -- mixes two colors by percentage where 100% 0929 keeps all of `color1`, and 0% keeps all of `color2`. Will take into account 0930 the alpha of the colors if it exists. See 0931 <http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#mix-instance_method>. 0932 0933 * `contrast(color, dark, light)` -- if `color` has a lightness value greater 0934 than 50% then `dark` is returned, otherwise return `light`. 0935 0936 * `extract(list, index)` -- returns the `index`th item from `list`. The list is 0937 `1` indexed, meaning the first item's index is 1, the second is 2, and etc. 0938 0939 * `pow(base, exp)` -- returns `base` raised to the power of `exp` 0940 0941 * `pi()` -- returns pi 0942 0943 * `mod(a,b)` -- returns `a` modulus `b` 0944 0945 * `tan(a)` -- returns tangent of `a` where `a` is in radians 0946 0947 * `cos(a)` -- returns cosine of `a` where `a` is in radians 0948 0949 * `sin(a)` -- returns sine of `a` where `a` is in radians 0950 0951 * `atan(a)` -- returns arc tangent of `a` 0952 0953 * `acos(a)` -- returns arc cosine of `a` 0954 0955 * `asin(a)` -- returns arc sine of `a` 0956 0957 * `sqrt(a)` -- returns square root of `a` 0958 0959 * `rgbahex(color)` -- returns a string containing 4 part hex color. 0960 0961 This is used to convert a CSS color into the hex format that IE's filter 0962 method expects when working with an alpha component. 0963 0964 ```less 0965 .class { 0966 @start: rgbahex(rgba(25, 34, 23, .5)); 0967 @end: rgbahex(rgba(85, 74, 103, .6)); 0968 // abridged example 0969 -ms-filter: 0970 e("gradient(start=@{start},end=@{end})"); 0971 } 0972 ``` 0973 0974 ## PHP Interface 0975 0976 When working with **lessphp** from PHP, the typical flow is to create a new 0977 instance of `lessc`, configure it how you like, then tell it to compile 0978 something using one built in compile methods. 0979 0980 Methods: 0981 0982 * [`compile($string)`](#compiling[) -- Compile a string 0983 0984 * [`compileFile($inFile, [$outFile])`](#compiling) -- Compile a file to another or return it 0985 0986 * [`checkedCompile($inFile, $outFile)`](#compiling) -- Compile a file only if it's newer 0987 0988 * [`cachedCompile($cacheOrFile, [$force])`](#compiling_automatically) -- Conditionally compile while tracking imports 0989 0990 * [`setFormatter($formatterName)`](#output_formatting) -- Change how CSS output looks 0991 0992 * [`setPreserveComments($keepComments)`](#preserving_comments) -- Change if comments are kept in output 0993 0994 * [`registerFunction($name, $callable)`](#custom_functions) -- Add a custom function 0995 0996 * [`unregisterFunction($name)`](#custom_functions) -- Remove a registered function 0997 0998 * [`setVariables($vars)`](#setting_variables_from_php) -- Set a variable from PHP 0999 1000 * [`unsetVariable($name)`](#setting_variables_from_php) -- Remove a PHP variable 1001 1002 * [`setImportDir($dirs)`](#import_directory) -- Set the search path for imports 1003 1004 * [`addImportDir($dir)`](#import_directory) -- Append directory to search path for imports 1005 1006 1007 ### Compiling 1008 1009 The `compile` method compiles a string of LESS code to CSS. 1010 1011 ```php 1012 <?php 1013 require "lessc.inc.php"; 1014 1015 $less = new lessc; 1016 echo $less->compile(".block { padding: 3 + 4px }"); 1017 ``` 1018 1019 The `compileFile` method reads and compiles a file. It will either return the 1020 result or write it to the path specified by an optional second argument. 1021 1022 ```php 1023 echo $less->compileFile("input.less"); 1024 ``` 1025 1026 The `compileChecked` method is like `compileFile`, but it only compiles if the output 1027 file doesn't exist or it's older than the input file: 1028 1029 ```php 1030 $less->checkedCompile("input.less", "output.css"); 1031 ``` 1032 1033 See [Compiling Automatically](#compiling_automatically) for a description of 1034 the more advanced `cachedCompile` method. 1035 1036 ### Output Formatting 1037 1038 Output formatting controls the indentation of the output CSS. Besides the 1039 default formatter, two additional ones are included and it's also easy to make 1040 your own. 1041 1042 To use a formatter, the method `setFormatter` is used. Just 1043 pass the name of the formatter: 1044 1045 ```php 1046 $less = new lessc; 1047 1048 $less->setFormatter("compressed"); 1049 echo $less->compile("div { color: lighten(blue, 10%) }"); 1050 ``` 1051 1052 In this example, the `compressed` formatter is used. The formatters are: 1053 1054 * `lessjs` *(default)* -- Same style used in LESS for JavaScript 1055 1056 * `compressed` -- Compresses all the unrequired whitespace 1057 1058 * `classic` -- **lessphp**'s original formatter 1059 1060 To revert to the default formatter, call `setFormatter` with a value of `null`. 1061 1062 #### Custom Formatter 1063 1064 The easiest way to customize the formatter is to create your own instance of an 1065 existing formatter and alter its public properties before passing it off to 1066 **lessphp**. The `setFormatter` method can also take an instance of a 1067 formatter. 1068 1069 Each of the formatter names corresponds to a class with `lessc_formatter_` 1070 prepended in front of it. Here the classic formatter is customized to use tabs 1071 instead of spaces: 1072 1073 1074 ```php 1075 $formatter = new lessc_formatter_classic; 1076 $formatter->indentChar = "\t"; 1077 1078 $less = new lessc; 1079 $less->setFormatter($formatter); 1080 echo $less->compileFile("myfile.less"); 1081 ``` 1082 1083 For more information about what can be configured with the formatter consult 1084 the source code. 1085 1086 ### Preserving Comments 1087 1088 By default, all comments in the source LESS file are stripped when compiling. 1089 You might want to keep the `/* */` comments in the output though. For 1090 example, bundling a license in the file. 1091 1092 Enable or disable comment preservation by calling `setPreserveComments`: 1093 1094 ```php 1095 $less = new lessc; 1096 $less->setPreserveComments(true); 1097 echo $less->compile("/* hello! */"); 1098 ``` 1099 1100 Comments are disabled by default because there is additional overhead, and more 1101 often than not they aren't needed. 1102 1103 1104 ### Compiling Automatically 1105 1106 Often, you want to only compile a LESS file only if it has been modified since 1107 last compile. This is very important because compiling is performance intensive 1108 and you should avoid a recompile if it possible. 1109 1110 The `checkedCompile` compile method will do just that. It will check if the 1111 input file is newer than the output file, or if the output file doesn't exist 1112 yet, and compile only then. 1113 1114 ```php 1115 $less->checkedCompile("input.less", "output.css"); 1116 ``` 1117 1118 There's a problem though. `checkedCompile` is very basic, it only checks the 1119 input file's modification time. It is unaware of any files from `@import`. 1120 1121 1122 For this reason we also have `cachedCompile`. It's slightly more complex, but 1123 gives us the ability to check changes to all files including those imported. It 1124 takes one argument, either the name of the file we want to compile, or an 1125 existing *cache object*. Its return value is an updated cache object. 1126 1127 If we don't have a cache object, then we call the function with the name of the 1128 file to get the initial cache object. If we do have a cache object, then we 1129 call the function with it. In both cases, an updated cache object is returned. 1130 1131 The cache object keeps track of all the files that must be checked in order to 1132 determine if a rebuild is required. 1133 1134 The cache object is a plain PHP `array`. It stores the last time it compiled in 1135 `$cache["updated"]` and output of the compile in `$cache["compiled"]`. 1136 1137 Here we demonstrate creating an new cache object, then using it to see if we 1138 have a recompiled version available to be written: 1139 1140 1141 ```php 1142 $inputFile = "myfile.less"; 1143 $outputFile = "myfile.css"; 1144 1145 $less = new lessc; 1146 1147 // create a new cache object, and compile 1148 $cache = $less->cachedCompile($inputFile); 1149 1150 file_put_contents($outputFile, $cache["compiled"]); 1151 1152 // the next time we run, write only if it has updated 1153 $last_updated = $cache["updated"]; 1154 $cache = $less->cachedCompile($cache); 1155 if ($cache["updated"] > $last_updated) { 1156 file_put_contents($outputFile, $cache["compiled"]); 1157 } 1158 1159 ``` 1160 1161 In order for the system to fully work, we must save cache object between 1162 requests. Because it's a plain PHP `array`, it's sufficient to 1163 [`serialize`](http://php.net/serialize) it and save it the string somewhere 1164 like a file or in persistent memory. 1165 1166 An example with saving cache object to a file: 1167 1168 ```php 1169 function autoCompileLess($inputFile, $outputFile) { 1170 // load the cache 1171 $cacheFile = $inputFile.".cache"; 1172 1173 if (file_exists($cacheFile)) { 1174 $cache = unserialize(file_get_contents($cacheFile)); 1175 } else { 1176 $cache = $inputFile; 1177 } 1178 1179 $less = new lessc; 1180 $newCache = $less->cachedCompile($cache); 1181 1182 if (!is_array($cache) || $newCache["updated"] > $cache["updated"]) { 1183 file_put_contents($cacheFile, serialize($newCache)); 1184 file_put_contents($outputFile, $newCache['compiled']); 1185 } 1186 } 1187 1188 autoCompileLess('myfile.less', 'myfile.css'); 1189 ``` 1190 1191 `cachedCompile` method takes an optional second argument, `$force`. Passing in 1192 true will cause the input to always be recompiled. 1193 1194 ### Error Handling 1195 1196 All of the compile methods will throw an `Exception` if the parsing fails or 1197 there is a compile time error. Compile time errors include things like passing 1198 incorrectly typed values for functions that expect specific things, like the 1199 color manipulation functions. 1200 1201 ```php 1202 $less = new lessc; 1203 try { 1204 $less->compile("} invalid LESS }}}"); 1205 } catch (Exception $ex) { 1206 echo "lessphp fatal error: ".$ex->getMessage(); 1207 } 1208 ``` 1209 ### Setting Variables From PHP 1210 1211 Before compiling any code you can set initial LESS variables from PHP. The 1212 `setVariables` method lets us do this. It takes an associative array of names 1213 to values. The values must be strings, and will be parsed into correct CSS 1214 values. 1215 1216 1217 ```php 1218 $less = new lessc; 1219 1220 $less->setVariables(array( 1221 "color" => "red", 1222 "base" => "960px" 1223 )); 1224 1225 echo $less->compile(".magic { color: @color; width: @base - 200; }"); 1226 ``` 1227 1228 If you need to unset a variable, the `unsetVariable` method is available. It 1229 takes the name of the variable to unset. 1230 1231 ```php 1232 $less->unsetVariable("color"); 1233 ``` 1234 1235 Be aware that the value of the variable is a string containing a CSS value. So 1236 if you want to pass a LESS string in, you're going to need two sets of quotes. 1237 One for PHP and one for LESS. 1238 1239 1240 ```php 1241 $less->setVariables(array( 1242 "url" => "'http://example.com.com/'" 1243 )); 1244 1245 echo $less->compile("body { background: url("@{url}/bg.png"); }"); 1246 ``` 1247 1248 ### Import Directory 1249 1250 When running the `@import` directive, an array of directories called the import 1251 search path is searched through to find the file being asked for. 1252 1253 By default, when using `compile`, the import search path just contains `""`, 1254 which is equivalent to the current directory of the script. If `compileFile` is 1255 used, then the directory of the file being compiled is used as the starting 1256 import search path. 1257 1258 Two methods are available for configuring the search path. 1259 1260 `setImportDir` will overwrite the search path with its argument. If the value 1261 isn't an array it will be converted to one. 1262 1263 1264 In this example, `@import "colors";` will look for either 1265 `assets/less/colors.less` or `assets/bootstrap/colors.less` in that order: 1266 1267 ```php 1268 $less->setImportDir(array("assets/less/", "assets/bootstrap")); 1269 1270 echo $less->compile('@import "colors";'); 1271 ``` 1272 1273 `addImportDir` will append a single path to the import search path instead of 1274 overwritting the whole thing. 1275 1276 ```php 1277 $less->addImportDir("public/stylesheets"); 1278 ``` 1279 1280 ### Custom Functions 1281 1282 **lessphp** has a simple extension interface where you can implement user 1283 functions that will be exposed in LESS code during the compile. They can be a 1284 little tricky though because you need to work with the **lessphp** type system. 1285 1286 The two methods we are interested in are `registerFunction` and 1287 `unregisterFunction`. `registerFunction` takes two arguments, a name and a 1288 callable value. `unregisterFunction` just takes the name of an existing 1289 function to remove. 1290 1291 Here's an example that adds a function called `double` that doubles any numeric 1292 argument: 1293 1294 ```php 1295 <?php 1296 include "lessc.inc.php"; 1297 1298 function lessphp_double($arg) { 1299 list($type, $value) = $arg; 1300 return array($type, $value*2); 1301 } 1302 1303 $less = new lessc; 1304 $less->registerFunction("double", "lessphp_double"); 1305 1306 // gives us a width of 800px 1307 echo $less->compile("div { width: double(400px); }"); 1308 ``` 1309 1310 The second argument to `registerFunction` is any *callable value* that is 1311 understood by [`call_user_func`](http://php.net/call_user_func). 1312 1313 If we are using PHP 5.3 or above then we are free to pass a function literal 1314 like so: 1315 1316 ```php 1317 $less->registerFunction("double", function($arg) { 1318 list($type, $value, $unit) = $arg; 1319 return array($type, $value*2, $unit); 1320 }); 1321 ``` 1322 1323 Now let's talk about the `double` function itself. 1324 1325 Although a little verbose, the implementation gives us some insight on the type 1326 system. All values in **lessphp** are stored in an array where the 0th element 1327 is a string representing the type, and the other elements make up the 1328 associated data for that value. 1329 1330 The best way to get an understanding of the system is to register is dummy 1331 function which does a `var_dump` on the argument. Try passing the function 1332 different values from LESS and see what the results are. 1333 1334 The return value of the registered function must also be a **lessphp** type, 1335 but if it is a string or numeric value, it will automatically be coerced into 1336 an appropriate typed value. In our example, we reconstruct the value with our 1337 modifications while making sure that we preserve the original type. 1338 1339 The instance of **lessphp** itself is sent to the registered function as the 1340 second argument in addition to the arguments array. 1341 1342 ## Command Line Interface 1343 1344 **lessphp** comes with a command line script written in PHP that can be used to 1345 invoke the compiler from the terminal. On Linux and OSX, all you need to do is 1346 place `plessc` and `lessc.inc.php` somewhere in your PATH (or you can run it in 1347 the current directory as well). On windows you'll need a copy of `php.exe` to 1348 run the file. To compile a file, `input.less` to CSS, run: 1349 1350 ```bash 1351 $ plessc input.less 1352 ``` 1353 1354 To write to a file, redirect standard out: 1355 1356 ```bash 1357 $ plessc input.less > output.css 1358 ``` 1359 1360 To compile code directly on the command line: 1361 1362 ```bash 1363 $ plessc -r "@color: red; body { color: @color; }" 1364 ``` 1365 1366 To watch a file for changes, and compile it as needed, use the `-w` flag: 1367 1368 ```bash 1369 $ plessc -w input-file output-file 1370 ``` 1371 1372 Errors from watch mode are written to standard out. 1373 1374 1375 ## License 1376 1377 Copyright (c) 2012 Leaf Corcoran, <http://leafo.net/lessphp> 1378 1379 Permission is hereby granted, free of charge, to any person obtaining 1380 a copy of this software and associated documentation files (the 1381 "Software"), to deal in the Software without restriction, including 1382 without limitation the rights to use, copy, modify, merge, publish, 1383 distribute, sublicense, and/or sell copies of the Software, and to 1384 permit persons to whom the Software is furnished to do so, subject to 1385 the following conditions: 1386 1387 The above copyright notice and this permission notice shall be 1388 included in all copies or substantial portions of the Software. 1389 1390 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 1391 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 1392 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 1393 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 1394 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 1395 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 1396 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 1397 1398 1399 *Also under GPL3 if required, see `LICENSE` file* 1400