Hacker News new | ask | show | jobs
by wwarneck 5518 days ago
The most common problem is conditional nesting, like with wrapper divs. In addition to :plain there is also :erb. I think the poster was implying that occasionally you'll need to structure code in a way that is not optimal (work arounds) to do something that requires no special work in erb.

I also wanted more specifics though, I felt like this was a fine opinion piece but lacked any specific world lessons.

1 comments

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.