Access to a property in a specific resource via sightly

谁都会走 提交于 2019-12-11 04:07:27

问题


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!


回答1:


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>



回答2:


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>


来源:https://stackoverflow.com/questions/39751000/access-to-a-property-in-a-specific-resource-via-sightly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!