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.

@vosechu
Copy link

vosechu commented Jul 11, 2012

Even though it's ugly this should be in the README. This isn't shameful, you do the best you can do with what you're given. If people don't like it they can go back to normal handlebars with it's ERBish syntax.

@jacquescrocker
Copy link

seems like it'd be trivial to make the first case work (which is much more readable). if the expression in express() is equal to 'else' then you don't need to output demarcation.last

what do you think? would you accept a small patch that enabled the follow?

= 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

shouldn't have any legacy code issues since this will only affect block expression that match 'else' (which at the moment, there should be exactly none of since it results in invalid handlebars syntax).

thanks!

@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