nonopolarity
nonopolarity

Reputation: 151046

Using HAML (in Ruby on Rails), is there a way to generate 50 nested DIVs?

To test something for CSS borders in nested DIVs, 50 DIVs need to be generated. In other words, generate

<div>
  <div>
    <div>
      ...  total 50 of them nested
    </div>
  </div>
</div>

because

- 50.times do
  %div 

won't do, as they all will be parallel, not nested.

Upvotes: 2

Views: 383

Answers (3)

Dty
Dty

Reputation: 12273

You're close. All you have to do is add 2 spaces for each iteration of the loop.

- 50.times do |i|
  ="#{' ' * (i*2)}"%div

The ' ' * (i*2) part is making use of the fact that strings can be multiplied.

Upvotes: 0

JCorcuera
JCorcuera

Reputation: 6834

You can try this:

module ApplicationHelper

  def recursive_divs(number)
    if number > 0
      number -= 1
      content_tag :div do
        recursive_divs(number)
      end
    end
  end

end

And use it on your view:

= recursive_divs(50)

hope this helps

Upvotes: 1

DonaldSowell
DonaldSowell

Reputation: 396

Simplistic, but seems to work.

- 50.times do
  <div>
- 50.times do
  </div>

v2 - Still simplistic, but gives more options.

- 50.times do |i|
  != "<div style='border-style:#{cycle('dotted','dashed')}'>".rjust(i+35)
  = i
- 50.downto(1) do |i|
  != "</div>".rjust(i+35) 

Upvotes: 4

Related Questions