The Hamlet template language reduces HTML syntax to its essential parts.
Ruby Emacs Lisp
Latest commit 3ed5548 Jun 15, 2012 @gregwebs skip failing tests
Failed to load latest commit information.
bin hamlet.rb overrides slim parser Oct 28, 2011
extra fix tests. just use a single space for whitespace Nov 23, 2011
lib do not blow up when required in an rails2 app Jun 15, 2012
.gitignore add gitignore Oct 28, 2011
Gemfile it works! Oct 28, 2011
LICENSE hamlet.rb overrides slim parser Oct 28, 2011 skip failing tests Jun 15, 2012
Rakefile hamlet.rb overrides slim parser Oct 28, 2011
hamlet.gemspec fix rcov dep on 1.9 Jun 15, 2012

Hamlet is a template language whose goal is to reduce HTML syntax to the essential parts.


    <p>Some paragraph.
    <ul data-attr=list>
        <li>Item 1
        <li>Item 2

That Hamlet snippet is equivalent to:

  <p>Some paragraph.</p>
  <ul data-attr="list">
    <li>Item 1</li>
    <li>Item 2</li>

see, it is just HTML! Designers love Hamlet because it is just HTML! Closing tags are inferred from whitespace.


You can see the original Haskell hamlet templating language and the javascript port.

This Hamlet (ruby) works on top of slim. Please take a look at the slim documentation if you are looking to see if a more advanced feature is supported.

Difference with Slim

The most important difference is that hamlet always uses angle brackets. Hamlet also does not require attributes to be quoted - unquoted is considered a normal html attribute value and quotes will automatically be added. Hamlet also uses a '#' for code comments and the normal <!-- for HTML comments. Hamlet also uses different whitespace indicators - see the next section.

In Slim you have:

/! HTML comment
p data-attr="foo" Text
  | More Text
  / Comment

In hamlet you have:

<!-- HTML comment
<p data-attr=foo>Text
  More Text
  # Comment


Using indentation does have some consequences with respect to white space. This library is designed to do the right thing most of the time. This is a slightly different design from the original Haskell implementation of Hamlet and Slim, but the same design as hamlet.js

A closing tag is placed immediately after the tag contents. If you want to have a space before a closing tag, use a comment sign # on the line to indicate where the end of the line is.

<b>spaces  # 2 spaces are included

A new line is automatically added after tags with inner text. If you have multiple lines of inner text without tags (not a common use case) they will also get a new line added. If you do not want white space, you point it out with a > character, that you could think of as the end of the last tag, although you can still use it when separating content without tags onto different lines. You can also use a > if you want more than one space.

<b>spaces  # 2 spaces are included
<b>spaces  </b>
<b>no space
>none after bold.
>  Two spaces after a period is bad!
<b>no space</b>none after bold.  Two spaces after a period is bad!

I18n support

You can hook up i18n support the same way you would for other templating lanugages. This rails plugin works out of the box.


A space is not automatically added after a tag when looping through an array Double quotes in attributes will get messed up: click=do('ok!') not click=do("whoops!")


Run tests with

bundle exec rake test


bundle exec ruby -r ./test/slim/helper.rb TEST

There are some failing tests that are skipped right now