mayu

Silent Haml control flow

Nested Haml under silent -scripts becomes rendered children. Childless scripts still run, but evaluate to niland add no child.

Childless setup code

- @setup_runs += 1

The side effect has run 1 times for this component instance.

The setup script itself produces no visible node.

Conditions

- if @show_if
  %p The if branch
- else
  %p The else branch

- unless @show_unless
  %p The unless branch
- else
  %p The unless else branch

The if branch

The unless branch

Case branches

- case @case_value
- when "alpha"
  %p Alpha selected
- when "beta"
  %p Beta selected
- else
  %p Fallback selected

Alpha selected

Iterator blocks

%ul
  - ITEMS.each do |item|
    %li= item

%ul
  - ITEMS.each { |item|
    %li= item
  • alpha
  • beta
  • gamma
  • alpha
  • beta
  • gamma

Ruby loops

- index = 0
- while index < ITEMS.length
  %span= ITEMS.fetch(index)
  - index += 1

- index = ITEMS.length
- until index.zero?
  - index -= 1
  %span= ITEMS.fetch(index)

- for item in ITEMS
  %span= item
alphabetagammagammabetaalphaalphabetagamma

Nested loops, next, and break

- ITEMS.each do |item|
  - [1, 2].each do |number|
    %span= "#{item}-#{number}"

- CONTROL_ITEMS.each do |item|
  - next if item == "skip"
  - break if item == "stop"
  %span= item
alpha-1alpha-2beta-1beta-2gamma-1gamma-2AB

Nested return

A separate component keeps an early return local to its own render method.

- ["A", "stop"].each do |item|
  - return if $stop && item == "stop"
    %p Stopped early
  %p= item

Stopped early