您可以通过一个简单的
Sling Model
和吊索
CompositeValueMap
CompositeValueMap文档状态:
基于两个ValueMap的ValueMap实现:-一个包含属性-另一个包含属性映射不包含值时使用的默认值。如果希望避免在多个资源上重复属性,可以使用CompositeValueMap来获取属性的串联映射。
我们可以通过提供子代的值映射(当前页的值映射),然后找到正确的祖先,并提供其属性值映射作为默认值来使用它。
就这个问题而言,我总是假设根的2rd后代永远是祖先(你可以根据你的要求找到你的祖先)
package com.sample.helpers;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.CompositeValueMap;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.OSGiService;
import org.apache.sling.models.annotations.injectorspecific.Self;
import javax.annotation.PostConstruct;
@Model(adaptables = Resource.class)
public class CustomInheritedPageProperties
{
ValueMap inheritedProperties;
@Self
Resource currentResource;
@OSGiService
PageManager pageManager;
@PostConstruct
protected void init() {
Page descendant = pageManager.getContainingPage(currentResource);
Page ancestor = descendant.getParent(2);
inheritedProperties = new CompositeValueMap(descendant.getProperties(), ancestor.getProperties());
}
public ValueMap getInheritedProperties()
{
return inheritedProperties;
}
}
现在您可以按如下方式使用它
<sly data-sly-use.propHelper="com.sample.helpers.CustomInheritedPageProperties">>/sly>
<h1>propHelper.inheritedProperties.someProp</h1>