Skip to content

Instantly share code, notes, and snippets.

@jimsynz
Created June 25, 2012 23:12
Show Gist options
  • Save jimsynz/2992040 to your computer and use it in GitHub Desktop.
Save jimsynz/2992040 to your computer and use it in GitHub Desktop.
Hamlbars nested conditions

You wrote:

= hb 'if content.condition1' do
  = hb 'if content.condition2' do
    %p Some paragraph
    = hb 'else'
      %p another paragraph
  = hb 'else'
    %p third paragraph

Correct code:

= hb 'if content.condition1' do
  = hb 'if content.condition2' do
    %p Some paragraph
    = hb 'else'
    %p another paragraph
  = hb 'else'
  %p third paragraph

The reason for this is actually a haml syntax issue, not a hamlbars one. You cannot nest under a ruby statement unless you're passing in a block. Since {{else}} is not a handlebars block statement you don't pass in a block. Haml would help a lot if it threw the a similar error to the one it throws when you try and nest under plain text.

@jacquescrocker
Copy link

correctly formatted

= hb 'if content.condition1' do
  = hb 'if content.condition2' do
    %p Some paragraph
    = hb 'else' do
      %p another paragraph
  = hb 'else' do
    %p third paragraph

@cybersquid
Copy link

I like the proposed syntax better. It seems much more natural.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment