<p>I have the need to display a nested set structure in HTML. I am doing it with the following partial:</p>

<pre><code>&lt;ul&lt;%= ' id="tree"' if depth == 0 %&gt;&gt;
&lt;% items.each do |item| %&gt;
  &lt;li id="node_&lt;%= item.id %&gt;"&gt;&lt;a&gt;&lt;%= item.name %&gt;&lt;/a&gt;
  &lt;% if item.has_children? %&gt;
    &lt;%= render :partial =&gt; 'tree_level', :locals =&gt; {:items =&gt; item.children, :depth =&gt; depth + 1} %&gt;
  &lt;% end %&gt;
  &lt;/li&gt;
&lt;% end %&gt;   
&lt;/ul&gt;
</code></pre>

<p>Is this the best place to have the code? I "feel" like there should be a <code>to_html</code> method on the object which dumps the entire tree structure for me, though this works.</p>

I am not sure whether it is best practice but I used similar code for rendering project tree.
Faster alternative is to create helper method doing the same job (recursively traversing tree and adding partial strings into result string). It is a little bit PHP style :( but for such a small amount of HTML is it OK, I guess :)
Helper looks like:
```
```
def render_node(node)
  res = "<ul>"
  ...
  node.items.each {|n| res << render_node(n)}
  ...
  res << "</ul>"
  res
end
```
```
Then it is used like this:
```
```
<%=render_node ProjectTree.new%>
```
```