Docly Developers
ExampleUpdated 17 Oct 2026

Recursive function

Example showing a recursive function (a function calling itself).

Example

Data

{
    "folders" :
    [
        {
            "name" : "Folder 1",
            "folders" :
            [
                {
                    "name" : "Folder 1.1",
                    "folders" : []
                }
            ]
        }
    ]
}

Code

Option 1 (##JS):
#for(let folder of folders) {#
    Folder: #docly.htmlEncode(folder.name)#<br>
    #for(let folder of folder.folders ?? []) {#{{current}}#}#
#}#


Option 2 (Pure JS):
#{
    function showFolders(list) {
        for(let folder of list) {
            write("Folder: " + docly.htmlEncode(folder.name) + "<br>");
            showFolders(folder.folders || []); // the function calls itself for the sub folders
        }
    }
    showFolders(folders);
}#

Output

Option 1 (#JS):
    Folder: Folder 1<br>
        Folder: Folder 1.1<br>
    



Option 2 (Pure JS):
Folder: Folder 1<br>Folder: Folder 1.1<br>