Sightly: list child nodes (+ error in docs?)

Avatar

Avatar

paro22

Avatar

paro22

paro22

15-10-2015

Hi,

I'm trying to list the child nodes of my current component's node and display their properties. This is my code:

<dl data-sly-list.slide="${currentNode.getNodes}"> <dt>index: ${slideList.index}</dt> <dd>title: ${slide.title}</dd> </dl>

It spits out the index correctly and when I do ${slide.path}, the path is correct as well. However, ${slide.title} and any other properties always remain empty, even though they do have values in the CRX.

 

Also for the docs [1], the last paragraph seems to have an error:

<dl data-sly-list.child="${myObj}"> <dt>key: ${item}</dt> <dd>value: ${myObj[item]}</dd> </dl>

My understanding is that if you use ".child" (or any other name) that replaces the "item", doesn't it?

 

Thanks,

Paul

 

[1] http://docs.adobe.com/docs/en/aem/6-0/develop/sightly.html#list

View Entire Topic

Avatar

Avatar

gabriel_walt

Employee

Avatar

gabriel_walt

Employee

gabriel_walt
Employee

15-10-2015

Hi Paul,

Resource properties can directly be accessed on the resource object itself and don't necessarily need to be accessed through the properties object:

These two expressions do the same: ${resource.sling:resourceType} ${properties.sling:resourceType}

But only properties can be iterated over, in case you wish to display all properties:

<dl data-sly-list="${properties}"> <dt>Key: ${item}</dt> <dd>Value: ${properties[item]}</dd> </dl>

And to display another resource's properties, you should access them via the resource as within the template you cannot adapt that other resource to a properties ValueMap:

<dl data-sly-list="${resource.listChildren}"> <dt>Node name: ${item.name}</dt> <dd>Title property: ${item.jcr:title}</dd> </dl>

This means that if you would like to iterate over the properties of another resource, you'd need this data to be prepared by the Use-API as you cannot do it within the template alone.

And one final note to your initial code example, where you use currentNode, you really should use resource instead. Because currentNode is coming from the low-level JCR API, and resource from the higher-level Sling API, and it is always encouraged to use the higher-level API if you have the choice. Sometimes the Sling API might do some node mapping on the Sling level to achieve some advanced site management features (like for AEM Launches), which are circumvented when using the JCR APIs. So only use the JCR APIs if you really know what you're doing, otherwise use the Sling APIs, which should allow you to do everything.

Best,
Gabriel