Examples
Contents
Example 1 — Get the Wave
HAML (written by a designer or programmer)
%h2 That's the wave - get it
%div
%p
indentation represents relation %p
is indented more than the %div
thus its a child of it.
Same applies to this text.
%p within a p tag
%p
PHP-TO-HAML gets out of the way.
It helps you focus on your real job rather
than on nasty PHP detailsHTML (generated by the resulting PHP code)
<h2>That's the wave - get it</h2> <div> <p>indentation represents relation %p is indented more than the %div thus its a child of it. Same applies to this text.</p> <p>within a p tag</p> <p>PHP-TO-HAML gets out of the way. It helps you focus on your real job rather than on nasty PHP details</p> </div>
Result
That's the wave - get it
indentation represents relation %p is indented more than the %div thus its a child of it. Same applies to this text.
within a p tag
PHP-TO-HAML gets out of the way. It helps you focus on your real job rather than on nasty PHP details
Example 2 — Automatic Quoting
HAML
%span='this is an arbitrary HTML expression'
%span='<> See how this gets quoted'
%span!='<b>unquoted bold text</b>'
%div
="this works :-)\n"
=strtoupper("this too")."\n"
interpolation quoted always eg #{'</>'}
%span='Note: '.substr('Ecomplicated php expression',1)HTML
<span>this is an arbitrary HTML expression</span> <span><> See how this gets quoted</span> <span><b>unquoted bold text</b></span> <div>this works :-) THIS TOO interpolation quoted always eg </> </div> <span>Note: complicated php expression</span>
Result
this is an arbitrary HTML expression
<> See how this gets quoted
unquoted bold text
<> See how this gets quoted
unquoted bold text
this works :-)
THIS TOO
interpolation quoted always eg </>
Note: complicated php expression
THIS TOO
interpolation quoted always eg </>
Example 3.1 — Filter: javascript
HAML
:javascript
// this code will be put into a JS tag
doSomething('foo');HTML
<script type='text/javascript'>
//<![CDATA[
// this code will be put into a JS tag
doSomething('foo');
//]]>
</script>Example 3.2 — Filter: css
HAML
:css // same for CSS rules
HTML
<style type='text/css'>
/*<![CDATA[*/
// same for CSS rules
/*]]>*/
</style>Example 3.3 — Filter: plain
HAML
%div
:plain
Indentation also applies to filters.
You can insert text verbatim here.
Text within filters may contain interpolations:
#{'<>'} #{3*7} #{count(array(1,2,3))}
No quoting takes place
<a href="www.google.com">GOOGLE</a>
unless you use the escaped filter:HTML
<div>Indentation also applies to filters. You can insert text verbatim here. Text within filters may contain interpolations: <> 21 3 No quoting takes place <a href="www.google.com">GOOGLE</a> unless you use the escaped filter: </div>
Example 3.4 — Filter: escaped
HAML
:escaped
<a href="www.google.com">GOOGLE</a>HTML
<a href="www.google.com">GOOGLE</a>
Example 4.1 — Loops: foreach
HAML
%h1 foreach
%ul
-foreach(array(1,2) as $nr)
%li=$nrHTML
<h1>foreach</h1> <ul> <li>1</li> <li>2</li> </ul>
Result
foreach
- 1
- 2
Example 4.2 — If / Then / Else
HAML
%h1 assign vars and add conditional HTML code
- $pred = true;
- if ($pred)
%div true
- else
%div
false (note that closing of else
branch depends on indentation only!)HTML
<h1>assign vars and add conditional HTML code</h1> <div>true</div>
Result
assign vars and add conditional HTML code
true
Example 4.3 — Loops: for
HAML
%h1 or use a simple for loop
- for($i=0; $i<3; $i++)
=$i.' 'HTML
<h1>or use a simple for loop</h1> 0 1 2
Result
or use a simple for loop
0 1 2Example 5 — How HAML-TO-PHP Handles Attributes
HAML
%h1
CSS selector like div attributes can
be provided this way:
#container.container
...
%h1 HTML like attributes
Note how duplicates are removed from the class attribute:
%a(class="foo bar foo") link
%a(href="# looser" href="# winner") link
%h1 Ruby Hash like attributes
Note how duplicates are removed from the class attribute:
%a{:href=>"#", :class=>["a", "list", "bar"]} link
%h1 You can also use interpolation within attribute names:
%div{"_#{3*7}" => "really weird attribute!"}
%h1 And of course you can mix both styles:
%h(target="_blank"){:href => "href value"} linkHTML
<h1>CSS selector like div attributes can be provided this way:</h1> <div class='container' id='container'>...</div> <h1>HTML like attributes</h1> Note how duplicates are removed from the class attribute: <a class='bar foo'>link</a> <a href='# winner'>link</a> <h1>Ruby Hash like attributes</h1> Note how duplicates are removed from the class attribute: <a class='a bar list' href='#'>link</a> <h1>You can also use interpolation within attribute names:</h1> <div _21='really weird attribute!'></div> <h1>And of course you can mix both styles:</h1> <h target='_blank' href='href value'>link</h>
Example 6 — Conditional Comments
HAML
This will only be rendered for IE: /[if IE] %p IE only -# This won't be rendered at all / this will be rendered as HTML comment
HTML
This will only be rendered for IE: <!--[if IE]> <p>IE only</p> <![endif]--> <!-- this will be rendered as HTML comment -->
Result
This will only be rendered for IE: