Hacker News new | ask | show | jobs
by snprbob86 5526 days ago
Conditional nesting is pretty easy using the "capture" method:

  - conditionallyNested = capture do
    .foo
      .bar
        baz
  - if f(x)
    .wrapper
      = conditionallyNested
  - else
    = conditionallyNested
Personally, I prefer this over duplicating the condition:

  <% condition = f(x) %>
  <% if condition %><div class="wrapper><% end %>
    <div class="foo"
      <div class="bar">
        baz
      </div>
    </div>
  <% if condition %></div><% end %>
However, you can trivially abstract my first example out into a helper and result in code looking like this:

  = wrap_if f(x), :class => 'wrapper' do
    .foo
      .bar
        baz
Much nicer looking, in my opinion. I don't consider this a workaround at all.