如何在 HAML 中编写 Ruby "if" 语句

How to write a Ruby "if" statement in HAML

- entries.each do |entry|
    - if !isOpen
      - columnClass = 'col-1'
    - if entry[:variant] == 'medium'
      - columnClas = 'col-2'
    - if entry[:variant] == 'larg'
      - columnClas = 'col-3'
    %div{:class => "GridTeaserArea--column #{columnClass}"}
      = component 'GridTeaser', entry

你很接近,你只需要更多缩进 if 和重新排列。

- entries.each do |entry|
    - if !isOpen
      - columnClass = 'col-1'
    - if entry[:variant] == 'medium'
      - columnClas = 'col-2'
    - if entry[:variant] == 'larg'
      - columnClas = 'col-3'
    %div{:class => "GridTeaserArea--column #{columnClass}"}
      = component 'GridTeaser', entry

按照您想到的逻辑更改代码。我试图展示如何在 HAML 中使用 if 条件。代码输出可能不是您想要的,因为它没有说明,我没有尝试假设它。

请阅读 HAML tutorial 以熟悉 HAML。

- isOpen = false
- entries.each do |entry|
 - if !isOpen
  - columnClass = 'col-1'
  :ruby
   if (entry[:variant] == 'medium')
    columnClass = 'col-2'
   end
   if (entry[:variant] == 'larg')
    columnClass = 'col-3'
   end
     = "<div class='GridTeaserArea--column #{columnClass}'>"

 = component 'GridTeaser', entry