Sunday, May 9, 2010

Haml For Grails

Writing a Grails app? Find out why so many Rubyists swear by Haml for writing views.

<!-- GSP/JSP -->
<div id="profile">
<div class="left column">
<div id="date"><%= date %></div>
<div id="address"><%= user.address %></div>
</div>
<div class="right column">
<div id="email"><%= user.email %></div>
<div id="bio"><%= user.bio %></div>
</div>
</div>


/ Haml
#profile
.left.column
#date= date
#address= user.address
.right.column
#email= user.email
#bio= user.bio

Interested parties have created JHaml, a Java implementation of Haml, and a corresponding Grails plugin. (Patches welcome!)

To try it out, just grab the plugin:
grails install-plugin haml
Then add this bean definition to your grails-app/conf/spring/resources.groovy.

beans = { // ...
groovyPageResourceLoader(com.cadrlife.jhaml.grailsplugin.HamlGroovyPageResourceLoader) {
baseResource = new org.springframework.core.io.FileSystemResource(".")
}
}

Now, you have the option of writing views (with the .haml extension) that will be automatically rendered to GSPs.

Learn more in the Haml tutorial.

Official Grails Plugin Page