通过可见访问特定资源中的属性 [英] 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}
谢谢!
推荐答案
开始-请注意,除非保证孩子的顺序,否则您正在使用sling:OrderedFolder或其他一些有序类型。因此,试图让第二个孩子变得毫无意义。
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.
话虽如此,我可能没有想到一些有效的用例,第二个孩子-据我所知,您将需要创建一个Java或JS对象并使用使用Api 。
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.
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>
这篇关于通过可见访问特定资源中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!