Archive for April, 2011

HAML Basics

Monday, April 25th, 2011

HAML (HTML Abstraction Markup Language) is a template engine responsible for addressing many of the flaws in traditional markup. HAML is Whitespace Active, DRY, follows CSS Conventions, and integrates Ruby or PHP code. Its results in very clean and elegant markup.

I will be focusing on the Ruby implementation in this posting, but note that there are also implementations for PHP, .NET, Java, and several other languages. As you might expect with Ruby, HAML is installed as a gem, and once installed, all .erb files can be renamed .HAML to be run through the HAML compiler.

When using HAML, tags are marked with the % symbol. For example:

%strong hello

This would be compiled to:

<strong>hello<strong>

Although you could write:

%div hello

to create a div, it is not necessary as HAML makes the common div tag the default. It also borrows the period symbol from CSS to denote class, so the following:

.content Hi

would be compiled to:

<div class='content'></div>

Classes can also be chained, so the following:

.content.notice Bye

would be compiled to:

<div class='content notice'>Bye</div>

HAML is whitespace active, so we can nest divs as follows:

#parent
  #child
    #grandchild
      #brother-twin I am the evil twin
      #sister-twin I am the good twin

to create:

<div id="parent">
  <div id="child">
    <div id="grandchild">
      <div id="brother-twin">I am the evil twin</div>
      <div id="sister-twin"> I am the good twin</div>
    </div>
  </div>
</div>

Comments are also more legible in HAML:

/ here is a comment

would generate the bulkier:

<!-- here is a comment -->

HAML includes filters which are denoted by a colon, and some examples are

:plain
:javascript
:markdown

You can also create your own. Usage is simple:

#content
  :markdown
    Some *emphasis*

would compile to:

<div id="content">
  <p>Some <em>emphasis</em></p>
</div>

Including Ruby (or other) code is either silent with the dash symbol, or inline with the equals symbol.

#content
  .title
    %h1=@title

would create:

<div id="content">
  <div class="title">
    <h1>My Page</h1>
  </div>
</div>

One of my favorites is the Object Reference, which uses square brackets with an object. Basically, given an object, it will work as div_for and content_for in Ruby, generating a id and class name based on the id and class of the given object. This is very handy for use with javascript and jquery and really polishes the markup.

%div[monster]
  %ul.eyeballs
    -monster.eyeballs.each do |eyeball|
      %li[eyeball]
        =eyeball.color
would create:
<div id="monster_4">
  <ul>
    <li id="eyeball_1">Green</li>
    <li id="eyeball_2">Blue</li>
    <li id="eyeball_3">Grey</li>
  </ul>
</div>
(B.S. Computer Science, Rutgers, 1995) Alex spent a few years programming games with LucasArts for the Saturn and Playstation platforms, before turning his attention to Web programming.

GORGES at CELEBRATION 2011 ENTREPRENEURSHIP@CORNELL

Wednesday, April 6th, 2011

GORGES is a Benefactor to this year’s ENTREPRENEURSHIP@CORNELL CELEBRATION 2011 on April 14 and 15. GORGES staff will be onsite through both days. Please say hello.

From 12:00 to 2:00 on the 15th, several of GORGES staff will attend the GORGES booth in the Statler Hotel Ballroom. This Technology, Business and Resource Expo is open to the public and an excellent opportunity to learn about the many GORGES projects developed for the Cornell Community.

Of course GORGES is interested in Cornell University as a client, not to mention its family and friend connections. Beyond that, many GORGES clients are entrepreneurs. During the first three months of this year GORGES has entertained three requests from entrepreneurs to invest computer programming sweat equity in their start-ups. GORGES is already engaged in equity arrangements with other entrepreneurs.

©2013 GORGES - All rights reserved
where programming meets design and lives happily ever after