Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

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

Avatar

Former Community Member

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

1 Accepted Solution

Avatar

Correct answer by
Level 1

Hi Paul,

Concerning your first question, your data-sly-list.slide statement results in the local identifier slide taking on the values iterated over by currentNode.getNodes()...in other words, at each iteration slide will be equal to a Node object. With slide.title, your therefore attempting to call getTitle() on a Node, which does not have that method.

About your second point, you are right, that was a mistake in the docs and it has been corrected. Thank you.

Cheers,

Peeter

View solution in original post

3 Replies

Avatar

Correct answer by
Level 1

Hi Paul,

Concerning your first question, your data-sly-list.slide statement results in the local identifier slide taking on the values iterated over by currentNode.getNodes()...in other words, at each iteration slide will be equal to a Node object. With slide.title, your therefore attempting to call getTitle() on a Node, which does not have that method.

About your second point, you are right, that was a mistake in the docs and it has been corrected. Thank you.

Cheers,

Peeter

Avatar

Former Community Member

Yes, I got that part. I guess where I'm stuck is, how do I get the properties of that node? Title was a bad example since I actually meant a 'title' property that I set myself. Let's say I want the property "imagePath" of the "slide" node. How would I go about retrieving that?

Looping through the properties works, but is there an easier way to access the properties directly? eg. ${slide.properties.imagePath} or something along those lines?

<dl data-sly-list.prop="${slide.getProperties}"> <dd>val: ${prop.name} --> ${prop.getString}</dd> </dl>

// Edit: Sorry Peeter for removing the 'solved' flag, just figured nobody will answer this if the post is resolved.

Avatar

Employee

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