PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/docs.html

https://github.com/conzi/lessphp
HTML | 347 lines | 273 code | 74 blank | 0 comment | 0 complexity | 366d447dbb21fc4ea7c4c0f8c93f6b27 MD5 | raw file
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  6. <link rel="stylesheet" type="text/css" href="style.css" media="screen" />
  7. <link rel="alternate" type="application/rss+xml" title="lessphp changelog feed" href="http://leafo.net/lessphp/feed/" />
  8. <title>Documentation - lessphp</title>
  9. </head>
  10. <body>
  11. <h1>Documentation - lessphp v0.2.0</h1>
  12. <div class="content">
  13. <ul id="nav">
  14. <li><a href="#start">Getting Started</a></li>
  15. <li><a href="#language">The Language</a>
  16. <ul><li><a href="#vars">Abstract Properties (Variables)</a></li></ul>
  17. <ul><li><a href="#pvalues">Property Values &amp; Expressions</a></li></ul>
  18. <ul><li><a href="#nested">Nested Blocks</a></li></ul>
  19. <ul><li><a href="#mixins">Mixins &amp; Namespace Access</a></li></ul>
  20. <ul><li><a href="#ablocks">Abstract Blocks</a></li></ul>
  21. <ul><li><a href="#args">Mixin Arguments</a></li></ul>
  22. <ul><li><a href="#import">Import Statement</a></li></ul>
  23. <ul><li><a href="#strings">String Mixins</a></li></ul>
  24. <ul><li><a href="#misc">Miscellaneous</a></li></ul>
  25. <ul><li><a href="#differences">Differences from Ruby LESS</a></li></ul>
  26. </li>
  27. <li><a href="#interface">The Interface</a></li>
  28. </ul>
  29. <p><strong>lessphp</strong> is a compiler that generates css from a small superset language that adds
  30. many additional features seen in other languages. It is based off an original Ruby implementation
  31. called <a href="http://lesscss.org/">LESS</a>. For the most part, <strong>lessphp</strong> is syntactically
  32. compatible with LESS, with the exception of a few things noted below.</p>
  33. <a name="start"></a>
  34. <h2>Getting Started</h2>
  35. <p>Download the latest version of <strong>lessphp</strong> <a href="http://leafo.net/lessphp/">here</a>.</p>
  36. <p>You can also find the latest development version in the <a href="http://github.com/leafo/lessphp">git repository</a>.</p>
  37. </div>
  38. <a name="language"></a>
  39. <h1>The Language</h1>
  40. <div class="content">
  41. <br />
  42. <p><strong>lessphp</strong> is a data description language built on top of CSS. The two major components of the language are
  43. blocks and property-value pairs. A block is a scope for a collection of property-value pairs.</p>
  44. <p>Blocks and properties have special characteristics depending on how they are named.</p>
  45. <p>It is important to realize that a block's state does not change over time. When a block is defined, all of its properties are constant. The best way to demonstrate this is to look at the following LESS snippet:</p>
  46. <pre class="code">
  47. body {
  48. color: @val;
  49. @val: blue;
  50. }</pre>
  51. <p>Because the state of the block is not changing over time, but constant after its creation,
  52. the color property is printed with the value <code>blue</code>.
  53. <a name="vars"></a>
  54. <h2>Abstract Properties (Variables)</h2>
  55. <p>Abstract properties are defined with a name starting with <code>@</code>. These types of properties are special in two ways: first, they are not included in the output of the compiler. Second, they can be easily accessed in the values of other properties just by writing their name. If a property is referenced but can not be found, a blank string is returned.</p>
  56. <p>As a LESS programmer, it means that you can define a collection of hidden values that can be referenced in other locations.</p>
  57. <pre class="code">
  58. @mycolor: #fff;
  59. body {
  60. color: @mycolor;
  61. }
  62. pre {
  63. color: @mycolor;
  64. }</pre>
  65. <p>Also take note that you can define abstract properties in the global scope.</p>
  66. <a name="pvalues"></a>
  67. <h2>Property Values & Expressions</h2>
  68. <p>All properties have at least one value. The value is a list of expressions separated by spaces or commas.
  69. An expression is any CSS value with optional mathematical operators applied to it. The operators only function
  70. with number and color value types.<p>
  71. <p>Operations on units will keep the unit of the rightmost value,
  72. unless it is unit-less, then the leftmost unit will be kept for the result. See the following examples below.<p>
  73. <pre class="code">body {
  74. color: #001 + #abc; // evaulates to #aabbdd:
  75. width: 3430px + 22; // evaluates to 3452px;
  76. margin: (1.5em / 2) + 2px; // evaluates to 2.85px;
  77. margin: 20 + 2px; // evaluates to 22px;
  78. font-family: "Some " + "Family"; // evaluates to "Some Family"
  79. }</pre>
  80. <p>It is important to notice that in the example above the output will print the margin property twice.
  81. A single property name can hold more than one value; older values are not overwritten.</p>
  82. <a name="nested"></a>
  83. <h2>Nested Blocks</h2>
  84. <p>Blocks can be nested inside of each other in order to achieve the same effect as listing out the
  85. path of identifiers in CSS. This can help to increase organization of your LESS code and reduce
  86. the amount of repeated tag names you have to type.</p>
  87. <pre class="code">body {
  88. a {
  89. color: green;
  90. :hover {
  91. color: blue;
  92. }
  93. }
  94. }</pre>
  95. <a name="mixins"></a>
  96. <h2>Mixins & Namespace Access</h2>
  97. <p>Any block can be "mixed" into the current block. This means that all properties and blocks
  98. in the target block are brought into the current block. It is possible to achieve the same effect
  99. with just CSS and HTML, but there are some additional features discussed below that make it worthwhile
  100. to utilize LESS-style mixing.</p>
  101. <p>The syntax is as follows:<p>
  102. <pre class="code">
  103. .myclass {
  104. @fonts: Helvetica, Arial;
  105. margin: 1.0em;
  106. line-spacing: 150%;
  107. a {
  108. background-color: black;
  109. }
  110. }
  111. pre {
  112. .myclass;
  113. font-family: @fonts; // uses the mixed in variable
  114. }
  115. div.notice {
  116. .myclass;
  117. }</pre>
  118. <p>If you want to mix in a specific block within another block you can use the <code>&gt;</code> namespace operator.
  119. Additionally you can pull out specific values of properties from blocks using the <code>[ ]</code> operator.
  120. <pre class="code">// using .myclass from above
  121. li {
  122. .myclass &gt; a; // just mix in properties from 'a' tag in .myclass
  123. fonts: .myclass[@fonts];
  124. padding: .myclass['margin'];
  125. }</pre>
  126. <a name="ablocks"></a>
  127. <h2>Abstract Blocks</h2>
  128. <p>Abstract blocks are like any other blocks, but their names start with a <code>@</code>. Like abstract properties, they are not
  129. included in the compiler's output. This allows you to utilize mixins without adding any unused blocks to the output.
  130. You can also use an abstract class to define a package of invisible, but extractable blocks and properties.<p>
  131. <pre class="code">@mypackage {
  132. .coolColors {
  133. color: pink;
  134. background-color: green;
  135. }
  136. .hotColors {
  137. color: red;
  138. background-color: orange;
  139. }
  140. }
  141. p {
  142. @mypackage &gt; .coolColors;
  143. }
  144. div {
  145. @mypackage; // inserts both classes into this block
  146. }</pre>
  147. <p>It is possible to give an abstract block the same name as an abstract property; their names will not collide.
  148. Block names and property names exist in different spaces. For example:</p>
  149. <pre class="code">@color(@color:red) { color: @color; } // this is valid
  150. </pre>
  151. <a name="args"></a>
  152. <h2>Mixin Arguments</h2>
  153. <p>All blocks have the option of taking argument lists, and the arguments can have default values.</p>
  154. <pre class="code">@some_mixin(@width: 200px;@radius) {
  155. border-radius: @radius;
  156. width: @width
  157. }
  158. .first {
  159. @some_mixin(300px; 2em);
  160. }
  161. .second {
  162. @some_mixin(;4px); // blank argument takes default value
  163. }</pre>
  164. <a name="import"></a>
  165. <h2>Import Statement</h2>
  166. <p>If you have multiple LESS files, you can combine them into a single CSS file during compilation using the
  167. <code>@import</code> directive. LESS import uses the same syntax as CSS import. If it can find the file specified
  168. then it will pull it into the compiler in place of the statement. If the file can't be found, the statement is
  169. printed to the output. The following are all valid:</p>
  170. <pre class="code">@import "file";
  171. @import 'file.less';
  172. @import url("file");
  173. @import url('file');
  174. @import url(file); </pre>
  175. <p>Note that if it fails to find a file it will append <code>.less</code> to the filename and try again.
  176. This means <code>@import 'somefile'</code> and <code>@import 'somefile.less'</code> will
  177. both import the file <code>somefile.less</code>.
  178. <a name="strings"></a>
  179. <h2>String Mixins</h2>
  180. <p>It is possible to access the value of an abstract property from a string using <code>{ }</code> operators.</p>
  181. <pre class="code">@image_folder: darktheme;
  182. .header {
  183. background-image: url(/images/{@image_folder}/header.png);
  184. }</pre>
  185. <p>The <code>{ }</code> syntax will also work in any string value type, which is any text
  186. wrapped in single or double quotes</p>
  187. <a name="misc"></a>
  188. <h2>Miscellaneous</h2>
  189. <p>As mentioned before, all properties hold constant data. This includes abstract values. While it may be convenient to
  190. think of them as variables, they don't have the ability to vary. For convenience, some tricks were implemented to
  191. make self referencing properties evaluate in order as if they had changing state. Consider the following statement:</p>
  192. <pre class="code">
  193. @what: 1;
  194. body {
  195. @what: @what + 1;
  196. @what: @what + 2;
  197. .class {
  198. @what: @what + 1;
  199. width: @what;
  200. }
  201. }
  202. #something {
  203. @what: 200;
  204. body > .class;
  205. }</pre>
  206. <p>In the output, <code>body .class</code> has width set to 5, and <code>#something</code> has width set to 201. It appears from that result that
  207. the property <code>@what</code> is being incremented. But, as mentioned above, the name <code>@what</code> stores a series of unchanging values.
  208. The values use delayed evaluation, so each value holds an equation.</p>
  209. <p>What this means is that the values of the properties don't change while the block is parsed, only additional property-value pairs are added
  210. to the block's definition.
  211. When the block's is compiled into CSS, the equations and property references are solved using the data in the scope. Technically, it is
  212. ambiguous what value to use for referencing a variable, because the single name can store multiple values.<p>
  213. <p>The approach taken in <strong>lessphp</strong> is to use the most recent value. This gives the appearance that the block is parsed line by line.
  214. In order to prevent infinite loops when a variable references itself, a single value in the set of all values for a given name can only be used once in
  215. the chain of dereferencing.</p>
  216. <a name="differences"></a>
  217. <h2>Differences from Ruby LESS</h2>
  218. <p class="important">Report missing ones <a href="http://github.com/leafo/lessphp/issues">here</a>.</p>
  219. <ul>
  220. <li>Arguments to mixins are separated by <code>;</code> instead of <code>,</code>.
  221. <p><b>Rationale</b>: the value of a css property can contain commas, so by using a comma as an argument separator you limit the kinds of input you can pass to a mixin.</p>
  222. </li>
  223. </ul>
  224. </div>
  225. <a name="interface"></a>
  226. <h1>The PHP Interface</h1><br />
  227. <div class="content">
  228. <p>There are a few ways to interface with the compiler. The easiest is to have it
  229. compile a LESS file when the page is requested. The static function
  230. <code>less::ccompile</code>, checked compile, will compile the inputed LESS file only when it
  231. is newer than the output file.<p>
  232. <pre class="code">
  233. <span class="PreProc">require</span> '<span class="String">lessc.inc.php</span>';
  234. <span class="Statement">try</span> <span class="Delimiter">{</span>
  235. lessc<span class="Operator">::</span>ccompile<span class="Delimiter">(</span>'<span class="String">input.less</span>', '<span class="String">out.css</span>'<span class="Delimiter">)</span>;
  236. <span class="Delimiter">}</span> <span class="Statement">catch</span> <span class="Delimiter">(</span><span class="Function">exception</span> <span class="Operator">$</span><span class="Identifier">ex</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
  237. <span class="Statement">exit</span><span class="Delimiter">(</span>'<span class="String">lessc fatal error:&lt;br /&gt;</span>'<span class="Operator">.</span><span class="Operator">$</span><span class="Identifier">ex</span><span class="Type">-&gt;</span>getMessage<span class="Delimiter">())</span>;
  238. <span class="Delimiter">}</span>
  239. </pre>
  240. <br />
  241. <p>Note that all failures with lessc are reported through exceptions.
  242. If you need more control then you can make your own instance of <code>lessc</code>.</p>
  243. <pre class="code">
  244. <span class="PreProc">require</span> '<span class="String">lessc.inc.php</span>';
  245. <span class="Operator">$</span><span class="Identifier">less</span> <span class="Operator">=</span> <span class="PreProc">new</span> lessc<span class="Delimiter">(</span>'<span class="String">path/to/style.less</span>'<span class="Delimiter">)</span>;
  246. <span class="Function">file_put_contents</span><span class="Delimiter">(</span>'<span class="String">path/to/style.css</span>', <span class="Operator">$</span><span class="Identifier">less</span><span class="Type">-&gt;</span>parse<span class="Delimiter">())</span>;
  247. </pre>
  248. <br />
  249. <p>In addition to loading from a file, you can also parse from a string like so:</p>
  250. <pre class="code">
  251. <span class="PreProc">require</span> '<span class="String">lessc.inc.php</span>';
  252. <span class="Operator">$</span><span class="Identifier">less</span> <span class="Operator">=</span> <span class="PreProc">new</span> lessc<span class="Delimiter">()</span>;
  253. <span class="Operator">$</span><span class="Identifier">style</span> <span class="Operator">=</span> '<span class="String">&lt;style type=&quot;text/css&quot;&gt;</span>'<span class="Operator">.</span>
  254. <span class="Operator">$</span><span class="Identifier">less</span><span class="Type">-&gt;</span>parse<span class="Delimiter">(</span>'<span class="String">.block { padding: 3 + 4px }</span>'<span class="Delimiter">)</span><span class="Operator">.</span>
  255. '<span class="String">&lt;/style&gt;</span>';
  256. </pre>
  257. <h2>Import Directoy</h2>
  258. <p>When using the <code>@import</code> directive, the compiler searches for files
  259. in the <code>$importDir</code> public property of <code>lessc</code>. If the compiler is loaded with a filename
  260. (either in the constructor or using <code>ccompile</code>) it will extract the directory and set that as the
  261. import directory</p>
  262. <h2>Library Functions</h2>
  263. <p>Functions within the compiler class that are prefixed with <code>lib_</code> are visible visible to the less
  264. code while it is being parsed. The easiest way to add your own functions is to subclass <code>lessc</code> and insert
  265. the implementations.</p>
  266. <br />
  267. <br />
  268. <hr />
  269. <p class="foot">
  270. <a href="http://leafo.net/lessphp/">http://leafo.net/lessphp</a> - Last updated March 9th 2010</p>
  271. </div>
  272. </body>
  273. </html>