I found a better approach, but it need a separate EJS file.
In my main.ejs
:
<%- partial('treeView', {items: tree}) %>
In the treeView.ejs
:
<% if (items.length) { %>
<ul>
<% } %>
<% items.forEach(function(item){ %>
<li>
<a href="<%= item.link %>"><%= item.name %></a>
<%- partial('treeView', {items: item.children}) %>
</li>
<% }) %>
<% if (items.length) { %>
</ul>
<% } %>