Haml Tips

Tadashi Shigeoka ·  Tue, December 18, 2012

Summary of Ruby on Rails + Haml tips, or things I often look up.

■ Setting class names with conditional branching

#someId{class: some_condition? && 'someClass'}

■ Issues with line breaks and spaces being inserted

Hamlでerbの「-%>」(直後の改行を出力しない)を実現するには? - QA@IT

■ Repeating ul, li

To output DOM with Haml where 3 li elements appear in a ul, then move to the next ul:

  • a
  • b
  • c
  • d
  • e
  • f

Using in_groups or in_groups_of, you can write it like this:

- @items.in_groups_of(3) do |item_group|
  %ul
    - item_group.each do |item|
      %li
        = item.name

in_groups (Array) - APIdock

in_groups_of (Array) - APIdock

That’s all from the Gemba.