How to write if-condition in Haml?

前端 未结 5 1416
长发绾君心
长发绾君心 2021-01-31 00:55

How to write if and if-else statements in Haml for a Ruby on Rails application?

5条回答
  •  隐瞒了意图╮
    2021-01-31 01:45

    HAML is indentation based , and the parser can be tricky.You don't need to use "- end" in Haml. Use indentation instead.In Haml,a block begins whenever the indentation is increased after a Ruby evaluation command. It ends when the indentation decreases.Sample if else block as follows.

    - if condition
      = something
    - else
      = something_else
    

    A practical example

    - if current_user
      = link_to 'Logout', logout_path
    - else
      = link_to 'Login', login_path
    

    Edit : If you just want to use if condition then

     - if current_user
      = link_to 'Logout', logout_path
    

提交回复
热议问题