Ayoub Abid
Ayoub Abid

Reputation: 452

Access to a property in a specific resource via sightly

I want to access to a specific property in resource.

The main resource hat two children and the app is in the first one. I want to get a property from the second child.

Can i find something like :

 ${resource.parent.child[1].valueMap.title} 

Thanks!

Upvotes: 2

Views: 3259

Answers (2)

Suren Konathala
Suren Konathala

Reputation: 3597

Here's another example that i used with converting the content to JSON. The contents of the JSON as parsed Objects and each Object has Attributes.

<div data-sly-use.jsonHelper="${'com.service.helpers.JSONHelper'
  @json=model.getRawJson}">

  ${jsonHelper.parsedJSON[item].commodityList[subitem].name}
  ...
</div>

Upvotes: -1

Jordan Shurmer
Jordan Shurmer

Reputation: 1066

To start - note that the order of the children may not be guaranteed, unless you're using sling:OrderedFolder or some other ordered type. So trying to get the "second" child may not even make sense.

Having said that, there may some valid use cases that I am not thinking of for needing to get the second child -- as far as I can tell you will need to create a Java or JS object and make use of the Use Api.

Simple Example Java object

package apps.your_app.components.yourComponent;

import com.adobe.cq.sightly.WCMUsePojo;
import org.apache.sling.api.resource.Resource;

import java.util.Iterator;

public class Model extends WCMUsePojo {

    @Override
    public void activate() throws Exception {
        //do some stuff if needed
    }

    public Resource getSecondSibling() {
        Resource parent = getResource().getParent();
        Resource secondSib = null;
        Iterator<Resource> children = parent.listChildren();

        //find the second child
        for (int i = 0; i < 2; i++)
            secondSib = children.next();
        return secondSib;
    }
}

Using it in the sightly:

<sly data-sly-use.model="Model">${model.secondSibling.propertyName}</sly>

Upvotes: 2

Related Questions