A
A
Anton Misyagin2015-05-30 05:22:17
Ruby on Rails
Anton Misyagin, 2015-05-30 05:22:17

How to properly set haml-rails indentation?

Just added the haml-rails gem.
Regenerated all views. He did not regenerate some of them due to errors (some unsupported characters). God bless him. I think that he did not - I will do it myself.
And because I am not strong in haml almost at once the question was formed. I have the following erb:

<% if condition1?%>
<div class="c1">
<% elsif condition2?%>
<div class="c2">
<% else %>
<div class="c3">
<% end %>
<div>Этот див вставлен в другой</div>
</div>

Without correcting the logic, I did this:
- if condition1?
  .c1
- elsif condition2?
  .c2
- else
  .c3
  %div
    Этот див вставлен в другой

Depending on the padding, I get a padding error, either the inner div is outside, or it appears in the markup inside only if condition1 and condition2 are not met. How is it usually practiced in such cases?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2015-05-30
@sunnmas

Haml has a strict hierarchy, so the last div must either be nested in all cases, or the class must be calculated separately, like so:

- klass = 'c3'
- if condition1?
  - klass = 'c1'
- elsif condition2?
  - klass = 'c2'
 

%div{class: klass}
  %div
    Этот див вставлен в другой

And in general this logic needs to be taken out ideally in helper.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question