Sunday 1 December 2013

Preprocessors- Session3

Filters

Haml provides a handful of filters, allowing different types of input to be used inside of Haml. Filters are identified with a colon followed by the name of the filter, :markdown for example, with all of the content to be filtered nested underneath.

Common Filters

Below are some of the more common filters, with the more popular ones of the group being :css and :javascript.
  • :cdata
  • :coffee
  • :css
  • :erb
  • :escaped
  • :javascript
  • :less
  • :markdown
  • :maruku
  • :plain
  • :preserve
  • :ruby
  • :sass
  • :scss
  • :textile

Javascript Filter

HAML
  1. :javascript
  2. $('button').on('click', function(event) {
  3. $('p').hide('slow');
  4. });
COMPILED HTML
  1. <script>
  2. $('button').on('click', function(event) {
  3. $('p').hide('slow');
  4. });
  5. </script>

CSS & Sass Filters

HAML
  1. CSS & SASS FILTERS:css
  2. .container {
  3. margin: 0 auto;
  4. width: 960px;
  5. }
  6. :sass
  7. .container
  8. margin: 0 auto
  9. width: 960px
COMPILED HTML
  1. CSS & SASS FILTERS<style>
  2. .container {
  3. margin: 0 auto;
  4. width: 960px;
  5. }
  6. </style>

Ruby Interpolation

As previously mentioned Haml can evaluate Ruby, and there may occasionally be times where Ruby needs to be evaluated inside of plain text. In this event Ruby needs to be interpolated, accomplished by wrapping the necessary Ruby code inside #{}.
Below is an example of Ruby being interpolated as part of a class name.
HAML
  1. %div{:class => "student-#{@student.name}"}
COMPILED HTML
  1. <div class="student-shay">