damoiser
damoiser

Reputation: 6238

Rails - print a "recursive" html variable in Slim

I would like to render in my view a "html"-content variable, let me explain:

Somewhere in a helper there is a pseudocode like this

# view helper for an ERB view

def render_something_recursively(i = 1)
  html = "<li>"
  html << "hi number #{i}"      

  if i < 1000
    html << render_something_recursively(i++)
  end

  html << "</li>"
end

Sorry for the bad example but I hope that it give you an idea, in facts I would like to iterate on a hierarchy structure (a tree) with flexible depth. For this reason I need a recursive method and I would like to keep it out from the view.

My question is: how can I accomplish to the same result but in Slim (or eventually in HAML)? How can I give the information of "indentation" at the htmlvariable?

Is it possible or I must use an ERB view?

My final goal should be "easily" something like that:

# recursive_list.html.slim
ul class="a_recursive_list"
 =render_something_recursively

Upvotes: 0

Views: 801

Answers (1)

dnatoli
dnatoli

Reputation: 7012

Use content_tag and it will render whatever templating engine you are using:

def render_something_recursively(i = 1)
  content_tag(:li, "hi number #{i}") do
    if i < 1000
      render_something_recursively(i++)
    end
  end
end

Additionally, I don't think you should nest <li> elements directly inside each other, you should either render text or a <ul> element nested inside the <li>.

Upvotes: 1

Related Questions