You need separate file rendering options, which recursively include:
<select> <option value="none">No parent</option> {% include 'options.html.twig' with {'folders': folders, 'level': 0} %} </select>
options.html.twig :
{% for folder in folders %} <option value="{{ folder.id }}"> {% for i in range(0, level) %} {% endfor %} {{ folder.name }} </option> {% include 'options.html.twig' with {'folders': folder.children, 'level': level + 1} %} {% endfor %}
I wrote this code right here, so don't expect it to be correct, but that should be enough to give you this idea.
source share