Matrix v5.4.5.1
Is it possible to create a hierarchical list of assets (and their children) with server side javascript?
For example, if my asset map looked like this:
- Item 1
- Item 1.1
- Item 1.2
- Item 1.3
- Item 2
- Item 3
I would like to be able to produce a marked up list like this:
<ul>
<li>Item 1
<ul>
<li>Item 1.1</li>
<li>Item 1.2</li>
<li>Item 1.3</li>
</ul>
</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
I’ve tried to do this with SSJ with no luck. I’m not sure if it’s because of the keywords execution order or some other issue, but it seems like it should be so simple. This is an example of what I’ve tried:
<ul>
<script runat="server">
const assets = %globals_asset_children_link_type_1:1234^as_asset:asset_assetid,asset_name^empty:[]%;
assets.forEach(function(asset) {
const assetChildren = %globals_asset_children_link_type_1:asset.asset_assetid^as_asset:asset_assetid,asset_name^empty:[]%;
print('<li>' + asset.asset_name);
if (assetChildren) {
print('<ul>');
assetChildren.forEach(function(assetChild) {
print('<li>' + assetChild.asset_name + '</li>');
}
print('</ul>');
}
print('</li>');
});
</script>
</ul>