PageRenderTime 76ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

Markdown | 298 lines | 221 code | 77 blank | 0 comment | 0 complexity | c23b131d33e33dcbeaa7141b6ff290e6 MD5 | raw file
  1. # Haml and Sass
  2. Haml and Sass are templating engines
  3. for the two most common types of documents on the web:
  4. HTML and CSS, respectively.
  5. They are designed to make it both easier and more pleasant
  6. to code HTML and CSS documents,
  7. by eliminating redundancy,
  8. reflecting the underlying structure that the document represents,
  9. and providing elegant, easily understandable, and powerful syntax.
  10. ## Using
  11. Haml and Sass can be used from the command line
  12. or as part of a Ruby web framework.
  13. The first step is to install the gem:
  14. gem install haml
  15. After you convert some HTML to Haml or some CSS to Sass,
  16. you can run
  17. haml document.haml
  18. sass style.scss
  19. to compile them.
  20. For more information on these commands, check out
  21. haml --help
  22. sass --help
  23. To install Haml and Sass in Rails 2,
  24. just add `config.gem "haml"` to `config/environment.rb`.
  25. In Rails 3, add `gem "haml"` to your Gemfile instead.
  26. and both Haml and Sass will be installed.
  27. Views with the `.html.haml` extension will automatically use Haml.
  28. Sass is a little more complicated;
  29. `.sass` files should be placed in `public/stylesheets/sass`,
  30. where they'll be automatically compiled
  31. to corresponding CSS files in `public/stylesheets` when needed
  32. (the Sass template directory is customizable...
  33. see [the Sass reference]( for details).
  34. For Merb, `.html.haml` views will work without any further modification.
  35. To enable Sass, you also need to add a dependency.
  36. To do so, just add
  37. dependency "merb-haml"
  38. to `config/dependencies.rb` (or `config/init.rb` in a flat/very flat Merb application).
  39. Then it'll work just like it does in Rails.
  40. Sass can also be used with any Rack-enabled web framework.
  41. To do so, just add
  42. require 'sass/plugin/rack'
  43. use Sass::Plugin::Rack
  44. to ``.
  45. Then any Sass files in `public/stylesheets/sass`
  46. will be compiled CSS files in `public/stylesheets` on every request.
  47. To use Haml and Sass programatically,
  48. check out the [YARD documentation](
  49. ## Formatting
  50. ### Haml
  51. The most basic element of Haml
  52. is a shorthand for creating HTML:
  53. %tagname{:attr1 => 'value1', :attr2 => 'value2'} Contents
  54. No end-tag is needed; Haml handles that automatically.
  55. If you prefer HTML-style attributes, you can also use:
  56. %tagname(attr1='value1' attr2='value2') Contents
  57. Adding `class` and `id` attributes is even easier.
  58. Haml uses the same syntax as the CSS that styles the document:
  59. %tagname#id.class
  60. In fact, when you're using the `<div>` tag,
  61. it becomes _even easier_.
  62. Because `<div>` is such a common element,
  63. a tag without a name defaults to a div. So
  64. #foo Hello!
  65. becomes
  66. <div id='foo'>Hello!</div>
  67. Haml uses indentation
  68. to bring the individual elements to represent the HTML structure.
  69. A tag's children are indented beneath than the parent tag.
  70. Again, a closing tag is automatically added.
  71. For example:
  72. %ul
  73. %li Salt
  74. %li Pepper
  75. becomes:
  76. <ul>
  77. <li>Salt</li>
  78. <li>Pepper</li>
  79. </ul>
  80. You can also put plain text as a child of an element:
  81. %p
  82. Hello,
  83. World!
  84. It's also possible to embed Ruby code into Haml documents.
  85. An equals sign, `=`, will output the result of the code.
  86. A hyphen, `-`, will run the code but not output the result.
  87. You can even use control statements
  88. like `if` and `while`:
  89. %p
  90. Date/Time:
  91. - now =
  92. %strong= now
  93. - if now > DateTime.parse("December 31, 2006")
  94. = "Happy new " + "year!"
  95. Haml provides far more tools than those presented here.
  96. Check out the [reference documentation](
  97. for full details.
  98. #### Indentation
  99. Haml's indentation can be made up of one or more tabs or spaces.
  100. However, indentation must be consistent within a given document.
  101. Hard tabs and spaces can't be mixed,
  102. and the same number of tabs or spaces must be used throughout.
  103. ### Sass
  104. Sass is an extension of CSS
  105. that adds power and elegance to the basic language.
  106. It allows you to use [variables][vars], [nested rules][nested],
  107. [mixins][mixins], [inline imports][imports],
  108. and more, all with a fully CSS-compatible syntax.
  109. Sass helps keep large stylesheets well-organized,
  110. and get small stylesheets up and running quickly,
  111. particularly with the help of
  112. [the Compass style library](
  113. [vars]:
  114. [nested]:
  115. [mixins]:
  116. [imports]:
  117. Sass has two syntaxes.
  118. The one presented here, known as "SCSS" (for "Sassy CSS"),
  119. is fully CSS-compatible.
  120. The other (older) syntax, known as the indented syntax or just "Sass",
  121. is whitespace-sensitive and indentation-based.
  122. For more information, see the [reference documentation][syntax].
  123. [syntax]:
  124. To run the following examples and see the CSS they produce,
  125. put them in a file called `test.scss` and run `sass test.scss`.
  126. #### Nesting
  127. Sass avoids repetition by nesting selectors within one another.
  128. The same thing works for properties.
  129. table.hl {
  130. margin: 2em 0;
  131. td.ln { text-align: right; }
  132. }
  133. li {
  134. font: {
  135. family: serif;
  136. weight: bold;
  137. size: 1.2em;
  138. }
  139. }
  140. #### Variables
  141. Use the same color all over the place?
  142. Need to do some math with height and width and text size?
  143. Sass supports variables, math operations, and many useful functions.
  144. $blue: #3bbfce;
  145. $margin: 16px;
  146. .content_navigation {
  147. border-color: $blue;
  148. color: darken($blue, 10%);
  149. }
  150. .border {
  151. padding: $margin / 2;
  152. margin: $margin / 2;
  153. border-color: $blue;
  154. }
  155. #### Mixins
  156. Even more powerful than variables,
  157. mixins allow you to re-use whole chunks of CSS,
  158. properties or selectors.
  159. You can even give them arguments.
  160. @mixin table-scaffolding {
  161. th {
  162. text-align: center;
  163. font-weight: bold;
  164. }
  165. td, th { padding: 2px; }
  166. }
  167. @mixin left($dist) {
  168. float: left;
  169. margin-left: $dist;
  170. }
  171. #data {
  172. @include left(10px);
  173. @include table-scaffolding;
  174. }
  175. A comprehensive list of features is available
  176. in the [Sass reference](
  177. ## Executables
  178. The Haml gem includes several executables that are useful
  179. for dealing with Haml and Sass from the command line.
  180. ### `haml`
  181. The `haml` executable transforms a source Haml file into HTML.
  182. See `haml --help` for further information and options.
  183. ### `sass`
  184. The `sass` executable transforms a source Sass file into CSS.
  185. See `sass --help` for further information and options.
  186. ### `html2haml`
  187. The `html2haml` executable attempts to transform HTML,
  188. optionally with ERB markup, into Haml code.
  189. Since HTML is so variable, this transformation is not always perfect;
  190. it's a good idea to have a human check the output of this tool.
  191. See `html2haml --help` for further information and options.
  192. ### `sass-convert`
  193. The `sass-convert` executable converts between CSS, Sass, and SCSS.
  194. When converting from CSS to Sass or SCSS,
  195. nesting is applied where appropriate.
  196. See `sass-convert --help` for further information and options.
  197. ## Authors
  198. Haml and Sass were created by [Hampton Catlin](
  199. (hcatlin) and he is the author of the original implementation. However, Hampton
  200. doesn't even know his way around the code anymore and now occasionally consults
  201. on the language issues. Hampton lives in Jacksonville, Florida and is the lead
  202. mobile developer for Wikimedia.
  203. [Nathan Weizenbaum]( is the primary developer and architect of
  204. the "modern" Ruby implementation of Haml. His hard work has kept the project
  205. alive by endlessly answering forum posts, fixing bugs, refactoring, finding
  206. speed improvements, writing documentation, implementing new features, and
  207. getting Hampton coffee (a fitting task for a boy-genius). Nathan lives in
  208. Seattle, Washington and while not being a student at the University of
  209. Washington or working at an internship, he consults for Unspace Interactive.
  210. [Chris Eppstein]( is a core contributor to
  211. Sass and the creator of Compass, the first Sass-based framework. Chris focuses
  212. on making Sass more powerful, easy to use, and on ways to speed its adoption
  213. through the web development community. Chris lives in San Jose, California with
  214. his wife and daughter. He is the Software Architect for
  215. [](, a website devoted to the 34 Million caregivers
  216. whose parents are sick or elderly, that uses Haml and Sass.
  217. If you use this software, you must pay Hampton a compliment. And
  218. buy Nathan some jelly beans. Maybe pet a kitten. Yeah. Pet that kitty.
  219. Some of the work on Haml was supported by Unspace Interactive.
  220. Beyond that, the implementation is licensed under the MIT License.
  221. Okay, fine, I guess that means compliments aren't __required__.