SlideShare une entreprise Scribd logo
1  sur  20
HAML For The Win! An introduction to HAML Oct. 13, 2011 1 Created for Magma Rails 2011 - www.magmarails.com Slides posted at http://tinyurl.com/magma-haml-sass Sample code from this presentation can be found in the following sample app: https://github.com/jonathandean/SASS-and-HAML-FTW
What is HAML? A more concise syntax for coding HTML in your Rails app Uses indentation for nesting markup Reduces code (no closing tags) Fixes the messy markup problem that often clutters views Uses a CSS-like syntax that is easier to read and compare to your style rules Makes coding your View layer faster Note: doesn’t affect render time or client-side performance, simply reduces coding efforts and makes the developer more efficient Easy to learn! Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 2
Using HAML in Rails Use it as a replacement for ERB files If you use Bundler, add this to your Gemfile: gem "haml” Files named filename.html.haml (instead of filename.html.erb) will be interpreted by HAML instead of ERB Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 3
A comparison HTML <body>   <div id=“wrapper”>     <div class=“stuff”>      <a href=“#”>Top</a>     </div>  </div> </body> HAML %body  #wrapper     .stuff       %a{:href => “#”} Top Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 4 7 lines 78 characters 4 lines 36 characters Note: Some examples compile using different formatting, I changed them for the slides for readability
Tag names Use % and then the name of the tag Examples: Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 5 HTML <body></body> <div></div> HAML %body %div
ID’s ID’s are just like they are in CSS #id_name Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 6 HTML <div id=“mine”></div> <p id=“yours”></p> HAML %div#mine %p#yours
Classes Classes are also just like they are in CSS .class_name Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 7 HTML <div class=“mine”></div> <p class=“yours”></p> HAML %div.mine %p.yours One way for multiple classes (more later) HTML <div class=“mine yours”></div> HAML %div.mine.yours
div is the default tag name If you want to leave out the tag name and just put classes and/or ID’s, %div is assumed Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 8 HTML <div id=“mine”></div> HAML %div#mine (or just) #mine
ID’s and Classes together Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 9 You can put ID’s and classes together as well HTML <div class=“yours” id=“mine”> </div> <div class=“yours his”id=“mine”> </div> HAML #mine.yours #mine.yours.his
Nesting Tags are nested by indentation only No closing tags! Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 10 HAML #mine  %p.yours hi! HTML <div id=“mine”> <p class=“yours”>hi!</p> </div>
Text content Text can go at the end of the line if there’s no other content to nest Can also be nested itself (required with other nested content) Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 11 HAML #mine  %p.yours hi! (or) #mine %p.yours    hi! #mine %p.yours    hi!    %span dude HTML <div id=“mine”>   <p class=“yours”>hi!</p> </div> <div id=“mine”>   <p class=“yours”>  hi! <span>dude</span>   </p> </div> #mine   %p.yourshi!     %span dude
Attributes Use a Ruby style hash for attributes Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 12 HTML <a href=“http://hi.com”>hi</a> <input type=“submit”> HAML %a{:href => ”http://hi.com”} hi %input{:type => "submit”} Or a more HTML-like syntax with () HAML %a(href=”http://hi.com”) hi %input(type="submit”) HTML <a href=“http://hi.com”>hi</a> <input type=“submit”>
Boolean Attributes Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 13 Use boolean values for attributes such as “selected” HTML <input selected> <input> HAML %input{:selected => true} (or) %input(selected=true) (or) %input(selected) %input{:selected => false} (or) %input(selected=false)
Using an array for ID’s and Classes For the ID attribute it will separate the values by an _ For the Class attribute it will add them as separate classes Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 14 HTML <p class=“one two”>hi</p> <p id=“one_two”>hi</p> HAML %p{:class => [‘one’,’two’]} hi %p{:id=> [‘one’,’two’]} hi
HTML?? You can also use regular HTML Converting a file over time Copy/paste a tracking code or something complicated and hard to write as HAML Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 15 HTML <div id=“myParagraph”>   <p>Hello there!</p> </div> HAML #myParagraph   <p>Hello there!</p>
Adding Ruby code Use – and = for Ruby code = for when you want to output the result – for when the code has no output Use of this should be rare, as this type of Ruby code shouldn’t often be in the view layer Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 16 HTML <p>hi hi hi hi hi</p> <p>hi</p> <p>hi</p> <p>hi</p> <p>hi</p> <p>hi</p> HAML %p= “hi “*5 ,[object Object],%p= “hi” No end required
Ruby Interpolation Use #{} to interpolate Ruby code Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 17 HTML <p>Hi Jon</p> HAML - awesome_guy = “Jon” %p Hi #{awesome_guy} (same as) ,[object Object],%p= “Hi #{awesome_guy}”
Filters Filters start with a : and let you put in indented content to be interpreted in a special way :javascriptfilter wraps some JavaScript in <script> and CDATA tags for inline JavaScript Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 18 HTML <script> //<![CDATA[ alert(‘Hello there!’); //]]> </script> HAML :javascript  alert(‘Hello there!’);
Filters :cssfilter wraps some CSS in <style> and CDATA tags for inline CSS Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 19 HTML <style> /*<![CDATA[*/ .mine{     color: red;   } /*]]>*/ </style> HAML :css  .mine{    color: red;  }
Other filters :plain – does not parse the content :escaped – same as plain but HTML-escapes the text :ruby – pass the content to the normal Ruby interpreter :sass – parse the content with SASS to produce CSS output and more! (see the docs) You can also make custom filters if you have special needs Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 20

Contenu connexe

Tendances

Tendances (19)

The Skinny on Slim
The Skinny on SlimThe Skinny on Slim
The Skinny on Slim
 
05 RD PoSD Tutorial_xhtml_to_html5_2016
05 RD PoSD Tutorial_xhtml_to_html5_201605 RD PoSD Tutorial_xhtml_to_html5_2016
05 RD PoSD Tutorial_xhtml_to_html5_2016
 
-Haml Presentation
-Haml Presentation-Haml Presentation
-Haml Presentation
 
Html in a box
Html in a boxHtml in a box
Html in a box
 
jQuery from the very beginning
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginning
 
HTML all tags .........its to much helpful for beginners
HTML all tags .........its to much helpful for beginners HTML all tags .........its to much helpful for beginners
HTML all tags .........its to much helpful for beginners
 
HTML Introduction
HTML IntroductionHTML Introduction
HTML Introduction
 
What's new in Rails 2?
What's new in Rails 2?What's new in Rails 2?
What's new in Rails 2?
 
HTML 5 Simple Tutorial Part 3
HTML 5 Simple Tutorial Part 3HTML 5 Simple Tutorial Part 3
HTML 5 Simple Tutorial Part 3
 
9. lower in Symfony 4
9. lower in Symfony 49. lower in Symfony 4
9. lower in Symfony 4
 
JavaScript - Part-1
JavaScript - Part-1JavaScript - Part-1
JavaScript - Part-1
 
Ruby on Rails
Ruby on RailsRuby on Rails
Ruby on Rails
 
Joomla security nuggets
Joomla security nuggetsJoomla security nuggets
Joomla security nuggets
 
Php 3 1
Php 3 1Php 3 1
Php 3 1
 
Haml & sass
Haml & sassHaml & sass
Haml & sass
 
Changing Template Engine
Changing Template EngineChanging Template Engine
Changing Template Engine
 
LESS is More
LESS is MoreLESS is More
LESS is More
 
Evolution of API With Blogging
Evolution of API With BloggingEvolution of API With Blogging
Evolution of API With Blogging
 
Html 101
Html 101Html 101
Html 101
 

Similaire à Introduction to HAML

Similaire à Introduction to HAML (20)

Xhtml Css Presentation
Xhtml Css PresentationXhtml Css Presentation
Xhtml Css Presentation
 
XHTML/CSS Presentation
XHTML/CSS PresentationXHTML/CSS Presentation
XHTML/CSS Presentation
 
XHTML/CSS Presentation
XHTML/CSS PresentationXHTML/CSS Presentation
XHTML/CSS Presentation
 
Diva
DivaDiva
Diva
 
Html1
Html1Html1
Html1
 
Html
HtmlHtml
Html
 
Learning HTML
Learning HTMLLearning HTML
Learning HTML
 
Introduction To Xml
Introduction To XmlIntroduction To Xml
Introduction To Xml
 
Web designing using html
Web designing using htmlWeb designing using html
Web designing using html
 
Xml Zoe
Xml ZoeXml Zoe
Xml Zoe
 
Xml Zoe
Xml ZoeXml Zoe
Xml Zoe
 
1.1 xhtml basics
1.1 xhtml basics1.1 xhtml basics
1.1 xhtml basics
 
Html
HtmlHtml
Html
 
Introduction to Web Design
Introduction to Web DesignIntroduction to Web Design
Introduction to Web Design
 
Html guide
Html guideHtml guide
Html guide
 
Class2
Class2Class2
Class2
 
Html For Beginners 2
Html For Beginners 2Html For Beginners 2
Html For Beginners 2
 
Block2 Session2 Presentation
Block2 Session2 PresentationBlock2 Session2 Presentation
Block2 Session2 Presentation
 
Lecture1 B Frames&Forms
Lecture1 B  Frames&FormsLecture1 B  Frames&Forms
Lecture1 B Frames&Forms
 
AK html
AK  htmlAK  html
AK html
 

Dernier

Top IPTV Subscription Service to Stream Your Favorite Shows in 2024.pdf
Top IPTV Subscription Service to Stream Your Favorite Shows in 2024.pdfTop IPTV Subscription Service to Stream Your Favorite Shows in 2024.pdf
Top IPTV Subscription Service to Stream Your Favorite Shows in 2024.pdfXtreame HDTV
 
Dubai Call girls Service 0524076003 Call girls in Dubai
Dubai Call girls Service 0524076003 Call girls in DubaiDubai Call girls Service 0524076003 Call girls in Dubai
Dubai Call girls Service 0524076003 Call girls in DubaiMonica Sydney
 
Haridwar Call Girls, 8699214473 Hot Girls Service Haridwar
Haridwar Call Girls, 8699214473 Hot Girls Service HaridwarHaridwar Call Girls, 8699214473 Hot Girls Service Haridwar
Haridwar Call Girls, 8699214473 Hot Girls Service Haridwarranekokila
 
Hire 💕 8617370543 Kushinagar Call Girls Service Call Girls Agency
Hire 💕 8617370543 Kushinagar Call Girls Service Call Girls AgencyHire 💕 8617370543 Kushinagar Call Girls Service Call Girls Agency
Hire 💕 8617370543 Kushinagar Call Girls Service Call Girls AgencyNitya salvi
 
Bhubaneswar🌹Call Girls Kalpana Mesuem ❤Komal 9777949614 💟 Full Trusted CALL ...
Bhubaneswar🌹Call Girls Kalpana Mesuem  ❤Komal 9777949614 💟 Full Trusted CALL ...Bhubaneswar🌹Call Girls Kalpana Mesuem  ❤Komal 9777949614 💟 Full Trusted CALL ...
Bhubaneswar🌹Call Girls Kalpana Mesuem ❤Komal 9777949614 💟 Full Trusted CALL ...Call Girls Mumbai
 
Just Call Vip call girls Palghar Escorts ☎️8617370543 Two shot with one girl ...
Just Call Vip call girls Palghar Escorts ☎️8617370543 Two shot with one girl ...Just Call Vip call girls Palghar Escorts ☎️8617370543 Two shot with one girl ...
Just Call Vip call girls Palghar Escorts ☎️8617370543 Two shot with one girl ...Nitya salvi
 
Mandvi (Ahemdabad) Escorts 6367492432 with Real Phone number and Model
Mandvi (Ahemdabad) Escorts 6367492432 with Real Phone number and ModelMandvi (Ahemdabad) Escorts 6367492432 with Real Phone number and Model
Mandvi (Ahemdabad) Escorts 6367492432 with Real Phone number and Modelhotbabesbook
 
Models in Deira 0567006274 Deira Call girl Service
Models in Deira 0567006274 Deira Call girl ServiceModels in Deira 0567006274 Deira Call girl Service
Models in Deira 0567006274 Deira Call girl ServiceMonica Sydney
 
Call Girls in Kollam - 9332606886 Our call girls are sure to provide you with...
Call Girls in Kollam - 9332606886 Our call girls are sure to provide you with...Call Girls in Kollam - 9332606886 Our call girls are sure to provide you with...
Call Girls in Kollam - 9332606886 Our call girls are sure to provide you with...call girls kolkata
 
Vip Call Girls Bhubaneswar 🐱‍🏍 9777949614 Independent Escorts Service Bhubane...
Vip Call Girls Bhubaneswar 🐱‍🏍 9777949614 Independent Escorts Service Bhubane...Vip Call Girls Bhubaneswar 🐱‍🏍 9777949614 Independent Escorts Service Bhubane...
Vip Call Girls Bhubaneswar 🐱‍🏍 9777949614 Independent Escorts Service Bhubane...Call Girls Mumbai
 
Gonda Nitya salvi 8617370543 VIP model college girls ...
Gonda Nitya salvi 8617370543 VIP model college girls ...Gonda Nitya salvi 8617370543 VIP model college girls ...
Gonda Nitya salvi 8617370543 VIP model college girls ...Nitya salvi
 
Deira Call girls Service 0507330913 Call girls in Deira
Deira Call girls Service 0507330913  Call girls in DeiraDeira Call girls Service 0507330913  Call girls in Deira
Deira Call girls Service 0507330913 Call girls in DeiraMonica Sydney
 
Call Girls Kozhikode - 9332606886 Our call girls are sure to provide you with...
Call Girls Kozhikode - 9332606886 Our call girls are sure to provide you with...Call Girls Kozhikode - 9332606886 Our call girls are sure to provide you with...
Call Girls Kozhikode - 9332606886 Our call girls are sure to provide you with...call girls kolkata
 
Call Girls in Perumbavoor / 9332606886 Genuine Call girls with real Photos an...
Call Girls in Perumbavoor / 9332606886 Genuine Call girls with real Photos an...Call Girls in Perumbavoor / 9332606886 Genuine Call girls with real Photos an...
Call Girls in Perumbavoor / 9332606886 Genuine Call girls with real Photos an...call girls kolkata
 
Hire 💕 8617370543 Mirzapur Call Girls Service Call Girls Agency
Hire 💕 8617370543 Mirzapur Call Girls Service Call Girls AgencyHire 💕 8617370543 Mirzapur Call Girls Service Call Girls Agency
Hire 💕 8617370543 Mirzapur Call Girls Service Call Girls AgencyNitya salvi
 
Call Girls in Ernakulam - 9332606886 Our call girls are sure to provide you w...
Call Girls in Ernakulam - 9332606886 Our call girls are sure to provide you w...Call Girls in Ernakulam - 9332606886 Our call girls are sure to provide you w...
Call Girls in Ernakulam - 9332606886 Our call girls are sure to provide you w...call girls kolkata
 
VIP Bhiwandi Phone 8250092165 Escorts Service +Call +Girls Along With Ac Room
VIP Bhiwandi Phone 8250092165 Escorts Service +Call +Girls Along With Ac RoomVIP Bhiwandi Phone 8250092165 Escorts Service +Call +Girls Along With Ac Room
VIP Bhiwandi Phone 8250092165 Escorts Service +Call +Girls Along With Ac Roommeghakumariji156
 
Pakistani Call girls in Ajman 0505086370 Ajman Call girls
Pakistani Call girls in Ajman 0505086370 Ajman Call girlsPakistani Call girls in Ajman 0505086370 Ajman Call girls
Pakistani Call girls in Ajman 0505086370 Ajman Call girlsMonica Sydney
 
Jann Mardenborough's Better Half in Racing and Life
Jann Mardenborough's Better Half in Racing and LifeJann Mardenborough's Better Half in Racing and Life
Jann Mardenborough's Better Half in Racing and Lifeget joys
 
Call 8617370543 Sangli Call girls with real photos and phone numbers
Call 8617370543 Sangli Call girls with real photos and phone numbersCall 8617370543 Sangli Call girls with real photos and phone numbers
Call 8617370543 Sangli Call girls with real photos and phone numbersNitya salvi
 

Dernier (20)

Top IPTV Subscription Service to Stream Your Favorite Shows in 2024.pdf
Top IPTV Subscription Service to Stream Your Favorite Shows in 2024.pdfTop IPTV Subscription Service to Stream Your Favorite Shows in 2024.pdf
Top IPTV Subscription Service to Stream Your Favorite Shows in 2024.pdf
 
Dubai Call girls Service 0524076003 Call girls in Dubai
Dubai Call girls Service 0524076003 Call girls in DubaiDubai Call girls Service 0524076003 Call girls in Dubai
Dubai Call girls Service 0524076003 Call girls in Dubai
 
Haridwar Call Girls, 8699214473 Hot Girls Service Haridwar
Haridwar Call Girls, 8699214473 Hot Girls Service HaridwarHaridwar Call Girls, 8699214473 Hot Girls Service Haridwar
Haridwar Call Girls, 8699214473 Hot Girls Service Haridwar
 
Hire 💕 8617370543 Kushinagar Call Girls Service Call Girls Agency
Hire 💕 8617370543 Kushinagar Call Girls Service Call Girls AgencyHire 💕 8617370543 Kushinagar Call Girls Service Call Girls Agency
Hire 💕 8617370543 Kushinagar Call Girls Service Call Girls Agency
 
Bhubaneswar🌹Call Girls Kalpana Mesuem ❤Komal 9777949614 💟 Full Trusted CALL ...
Bhubaneswar🌹Call Girls Kalpana Mesuem  ❤Komal 9777949614 💟 Full Trusted CALL ...Bhubaneswar🌹Call Girls Kalpana Mesuem  ❤Komal 9777949614 💟 Full Trusted CALL ...
Bhubaneswar🌹Call Girls Kalpana Mesuem ❤Komal 9777949614 💟 Full Trusted CALL ...
 
Just Call Vip call girls Palghar Escorts ☎️8617370543 Two shot with one girl ...
Just Call Vip call girls Palghar Escorts ☎️8617370543 Two shot with one girl ...Just Call Vip call girls Palghar Escorts ☎️8617370543 Two shot with one girl ...
Just Call Vip call girls Palghar Escorts ☎️8617370543 Two shot with one girl ...
 
Mandvi (Ahemdabad) Escorts 6367492432 with Real Phone number and Model
Mandvi (Ahemdabad) Escorts 6367492432 with Real Phone number and ModelMandvi (Ahemdabad) Escorts 6367492432 with Real Phone number and Model
Mandvi (Ahemdabad) Escorts 6367492432 with Real Phone number and Model
 
Models in Deira 0567006274 Deira Call girl Service
Models in Deira 0567006274 Deira Call girl ServiceModels in Deira 0567006274 Deira Call girl Service
Models in Deira 0567006274 Deira Call girl Service
 
Call Girls in Kollam - 9332606886 Our call girls are sure to provide you with...
Call Girls in Kollam - 9332606886 Our call girls are sure to provide you with...Call Girls in Kollam - 9332606886 Our call girls are sure to provide you with...
Call Girls in Kollam - 9332606886 Our call girls are sure to provide you with...
 
Vip Call Girls Bhubaneswar 🐱‍🏍 9777949614 Independent Escorts Service Bhubane...
Vip Call Girls Bhubaneswar 🐱‍🏍 9777949614 Independent Escorts Service Bhubane...Vip Call Girls Bhubaneswar 🐱‍🏍 9777949614 Independent Escorts Service Bhubane...
Vip Call Girls Bhubaneswar 🐱‍🏍 9777949614 Independent Escorts Service Bhubane...
 
Gonda Nitya salvi 8617370543 VIP model college girls ...
Gonda Nitya salvi 8617370543 VIP model college girls ...Gonda Nitya salvi 8617370543 VIP model college girls ...
Gonda Nitya salvi 8617370543 VIP model college girls ...
 
Deira Call girls Service 0507330913 Call girls in Deira
Deira Call girls Service 0507330913  Call girls in DeiraDeira Call girls Service 0507330913  Call girls in Deira
Deira Call girls Service 0507330913 Call girls in Deira
 
Call Girls Kozhikode - 9332606886 Our call girls are sure to provide you with...
Call Girls Kozhikode - 9332606886 Our call girls are sure to provide you with...Call Girls Kozhikode - 9332606886 Our call girls are sure to provide you with...
Call Girls Kozhikode - 9332606886 Our call girls are sure to provide you with...
 
Call Girls in Perumbavoor / 9332606886 Genuine Call girls with real Photos an...
Call Girls in Perumbavoor / 9332606886 Genuine Call girls with real Photos an...Call Girls in Perumbavoor / 9332606886 Genuine Call girls with real Photos an...
Call Girls in Perumbavoor / 9332606886 Genuine Call girls with real Photos an...
 
Hire 💕 8617370543 Mirzapur Call Girls Service Call Girls Agency
Hire 💕 8617370543 Mirzapur Call Girls Service Call Girls AgencyHire 💕 8617370543 Mirzapur Call Girls Service Call Girls Agency
Hire 💕 8617370543 Mirzapur Call Girls Service Call Girls Agency
 
Call Girls in Ernakulam - 9332606886 Our call girls are sure to provide you w...
Call Girls in Ernakulam - 9332606886 Our call girls are sure to provide you w...Call Girls in Ernakulam - 9332606886 Our call girls are sure to provide you w...
Call Girls in Ernakulam - 9332606886 Our call girls are sure to provide you w...
 
VIP Bhiwandi Phone 8250092165 Escorts Service +Call +Girls Along With Ac Room
VIP Bhiwandi Phone 8250092165 Escorts Service +Call +Girls Along With Ac RoomVIP Bhiwandi Phone 8250092165 Escorts Service +Call +Girls Along With Ac Room
VIP Bhiwandi Phone 8250092165 Escorts Service +Call +Girls Along With Ac Room
 
Pakistani Call girls in Ajman 0505086370 Ajman Call girls
Pakistani Call girls in Ajman 0505086370 Ajman Call girlsPakistani Call girls in Ajman 0505086370 Ajman Call girls
Pakistani Call girls in Ajman 0505086370 Ajman Call girls
 
Jann Mardenborough's Better Half in Racing and Life
Jann Mardenborough's Better Half in Racing and LifeJann Mardenborough's Better Half in Racing and Life
Jann Mardenborough's Better Half in Racing and Life
 
Call 8617370543 Sangli Call girls with real photos and phone numbers
Call 8617370543 Sangli Call girls with real photos and phone numbersCall 8617370543 Sangli Call girls with real photos and phone numbers
Call 8617370543 Sangli Call girls with real photos and phone numbers
 

Introduction to HAML

  • 1. HAML For The Win! An introduction to HAML Oct. 13, 2011 1 Created for Magma Rails 2011 - www.magmarails.com Slides posted at http://tinyurl.com/magma-haml-sass Sample code from this presentation can be found in the following sample app: https://github.com/jonathandean/SASS-and-HAML-FTW
  • 2. What is HAML? A more concise syntax for coding HTML in your Rails app Uses indentation for nesting markup Reduces code (no closing tags) Fixes the messy markup problem that often clutters views Uses a CSS-like syntax that is easier to read and compare to your style rules Makes coding your View layer faster Note: doesn’t affect render time or client-side performance, simply reduces coding efforts and makes the developer more efficient Easy to learn! Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 2
  • 3. Using HAML in Rails Use it as a replacement for ERB files If you use Bundler, add this to your Gemfile: gem "haml” Files named filename.html.haml (instead of filename.html.erb) will be interpreted by HAML instead of ERB Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 3
  • 4. A comparison HTML <body> <div id=“wrapper”> <div class=“stuff”> <a href=“#”>Top</a> </div> </div> </body> HAML %body #wrapper .stuff %a{:href => “#”} Top Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 4 7 lines 78 characters 4 lines 36 characters Note: Some examples compile using different formatting, I changed them for the slides for readability
  • 5. Tag names Use % and then the name of the tag Examples: Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 5 HTML <body></body> <div></div> HAML %body %div
  • 6. ID’s ID’s are just like they are in CSS #id_name Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 6 HTML <div id=“mine”></div> <p id=“yours”></p> HAML %div#mine %p#yours
  • 7. Classes Classes are also just like they are in CSS .class_name Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 7 HTML <div class=“mine”></div> <p class=“yours”></p> HAML %div.mine %p.yours One way for multiple classes (more later) HTML <div class=“mine yours”></div> HAML %div.mine.yours
  • 8. div is the default tag name If you want to leave out the tag name and just put classes and/or ID’s, %div is assumed Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 8 HTML <div id=“mine”></div> HAML %div#mine (or just) #mine
  • 9. ID’s and Classes together Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 9 You can put ID’s and classes together as well HTML <div class=“yours” id=“mine”> </div> <div class=“yours his”id=“mine”> </div> HAML #mine.yours #mine.yours.his
  • 10. Nesting Tags are nested by indentation only No closing tags! Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 10 HAML #mine %p.yours hi! HTML <div id=“mine”> <p class=“yours”>hi!</p> </div>
  • 11. Text content Text can go at the end of the line if there’s no other content to nest Can also be nested itself (required with other nested content) Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 11 HAML #mine %p.yours hi! (or) #mine %p.yours hi! #mine %p.yours hi! %span dude HTML <div id=“mine”> <p class=“yours”>hi!</p> </div> <div id=“mine”> <p class=“yours”> hi! <span>dude</span> </p> </div> #mine %p.yourshi! %span dude
  • 12. Attributes Use a Ruby style hash for attributes Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 12 HTML <a href=“http://hi.com”>hi</a> <input type=“submit”> HAML %a{:href => ”http://hi.com”} hi %input{:type => "submit”} Or a more HTML-like syntax with () HAML %a(href=”http://hi.com”) hi %input(type="submit”) HTML <a href=“http://hi.com”>hi</a> <input type=“submit”>
  • 13. Boolean Attributes Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 13 Use boolean values for attributes such as “selected” HTML <input selected> <input> HAML %input{:selected => true} (or) %input(selected=true) (or) %input(selected) %input{:selected => false} (or) %input(selected=false)
  • 14. Using an array for ID’s and Classes For the ID attribute it will separate the values by an _ For the Class attribute it will add them as separate classes Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 14 HTML <p class=“one two”>hi</p> <p id=“one_two”>hi</p> HAML %p{:class => [‘one’,’two’]} hi %p{:id=> [‘one’,’two’]} hi
  • 15. HTML?? You can also use regular HTML Converting a file over time Copy/paste a tracking code or something complicated and hard to write as HAML Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 15 HTML <div id=“myParagraph”> <p>Hello there!</p> </div> HAML #myParagraph <p>Hello there!</p>
  • 16.
  • 17.
  • 18. Filters Filters start with a : and let you put in indented content to be interpreted in a special way :javascriptfilter wraps some JavaScript in <script> and CDATA tags for inline JavaScript Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 18 HTML <script> //<![CDATA[ alert(‘Hello there!’); //]]> </script> HAML :javascript alert(‘Hello there!’);
  • 19. Filters :cssfilter wraps some CSS in <style> and CDATA tags for inline CSS Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 19 HTML <style> /*<![CDATA[*/ .mine{ color: red; } /*]]>*/ </style> HAML :css .mine{ color: red; }
  • 20. Other filters :plain – does not parse the content :escaped – same as plain but HTML-escapes the text :ruby – pass the content to the normal Ruby interpreter :sass – parse the content with SASS to produce CSS output and more! (see the docs) You can also make custom filters if you have special needs Oct. 13, 2011 An Introduction to HAML by Jonathan Dean - www.jonathandean.com 20