Haml says yes, rails says no

I’m using the haml gem and it’s great, been using it for a while and I
love it.

I’ve got a problem trying to display the flash messages, if I do the
following:

 - if flash[:notice]
   #flashNotice
     =flash[:notice]
 - else if flash[:error]
   #flashError
     =flash[:error]
 - end

I get

You don’t need to use “- end” in Haml. Use indentation instead:

  • if foo?
    %strong Foo!
  • else
    Not foo.

Ok, so I take out “- end” and I get

default.haml:21: syntax error, unexpected $end, expecting kEND

So which is it? What do I have to do to get around this? My
indentation is perfect, so I don’t see what else I can do.
Incidentally this appears to be a new problem with the latest version
of haml. Before I was using “- end” and it worked perfectly.

Any help appreciated.

Thanks

Matt

I realize this isn’t a direct answer to your question but to give you
some context I have been using haml since it was first released and I
have used in on many many projects. I absolutely cannot go back to rhtml
because haml is so much more concise. Still though, I have no idea the
answer to your question. Why? Because I had always heard that
conditional logic should not be in my views. I realize this is a
personal opinion but I figure I would show you how I would handle this:

application_helper.rb

even if flash error doesn’t exist this will return nil (empty as

expected)
def place_flash_message
flash[:notice] ? flash[:notice] : flash[:error]
end

view.html.haml

= place_flash_message

or if I really need conditional logic such as in:

if logged_in?
something
else
other thing

I would simply create a helper or a helper block with capture.

Anyways, in my opinion this improves readability and as a general rule
of thumb in 2 years of rails development projects I have never felt the
need to stick conditional logic directly in my view.

Now assuming you discard all that, I think the correct method is not to
put the end in haml which would make more sense in the haml way of doing
things. Since I haven’t actually needed to do this, I am not sure the
issue. Good luck

Matt H. wrote:

 - if flash[:notice]
   #flashNotice
     =flash[:notice]
 - else if flash[:error]
   #flashError
     =flash[:error]
 - end

What about

  • if flash[:notice]
    #flashNotice
    = flash[:notice]
  • elsif flash[:error]

    flashError

    = flash[:error]

Your “else if” may be constructing an inner “if” whose indentation
doesn’t resolve properly

Ar Chron wrote:

What about

  • if flash[:notice]
    #flashNotice
    = flash[:notice]
  • elsif flash[:error]

    flashError

    = flash[:error]

Your “else if” may be constructing an inner “if” whose indentation
doesn’t resolve properly

Yes elsif solves it, I must’ve been using too many other languages
recently :smiley:

Thanks

Matt