How to generate nested divs in loop?

47 views
Skip to first unread message

mongo...@gmail.com

unread,
Jan 15, 2016, 8:46:52 AM1/15/16
to Haml

Is there any way to generate many divs nested each other?

I expect print some like this:

<div>
  <div>
    <div>
      <div>
        <div>
        </div>
      </div>
    </div>
  </div>
</div>

Simple loop is not helping

- (1..5).each do |i|
  %div

The result is:

<div></div>
<div></div>
<div></div>
<div></div>
<div></div>

engineerDave

unread,
Jan 15, 2016, 10:47:33 AM1/15/16
to Haml
You'll need recursion not iteration. 

Basically you write a method that calls itself n number of times and has an exit condition to avoid a race condition. This is a good exercise for a beginning programmer, also its surprising how many programmers don't know it. So I'll leave it to you as homework and also because I'm at work and don't have time. good luck
Reply all
Reply to author
Forward
0 new messages