inheritedpageproperties helps you retrieve a parent property from parent pages. Inherited page properties utilises com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap, where the search is made on the page hierarchy, upwards; searching children of the page nodes (components) are not included. Inherited page properties can be retrieved in multiple ways, where this article will only focus on 3 ways.
In this article, we will go through the 3 different ways in how we can retrieve inherited page properties.
- How does this work?
- inheritedPageProperties with Sightly
- inheritedPageProperties with JSP
- inheritedPageProperties with OSGI Bundle, Java Backend
How does this work?
An InheritanceValueMap for a given Resource that will inherit values from ancestral pages.
For example, given: /content/parent/page/jcr:content/footer/image/@width, the HierarchyNodeInheritanceValueMap will search for a footer/image/@width property in:
look up one: /content/parent/currentPage/jcr:content/footer/image/@width
look up two: /content/parent/jcr:content/footer/image/@width
look up three: /content/jcr:content/footer/image/@width
1. inheritedPageProperties with Sightly
Sightly offers a plethora of AEM Global objects, and one of them notably is the “inheritedPageProperties”. Utilising the inheritedPageProperties global object, we can use dot or square bracket notation to access the interited page property value.
1 2 3 4 | ${inheritedPageProperties.myCustomProperty} ${inheritedPageProperties['myCustomProperty']} ${inheritedPageProperties.jcr:title} ${inheritedPageProperties['jcr:title']} |
2. inheritedPageProperties with JSP
Utilising the JSP include, /libs/foundation/global.jsp (purpose of retreiving the currentPage AEM global object), we can make use of the HierarchyNodeInheritanceValueMap.class, and currentPage resource to initialise the InheritanceValueMap.class object. Next, calling the method getInherited(String name, Class<T> type), we are able to get the inherited page property.
1 2 3 4 5 6 7 8 9 10 11 | <%@include file="/libs/foundation/global.jsp"%> <%@ page import="com.day.cq.commons.inherit.InheritanceValueMap" %> <%@ page import="com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap" %> <% InheritanceValueMap ivm = new HierarchyNodeInheritanceValueMap(currentPage.getContentResource()); String inheritedValueMyCustomProperty = ivm.getInherited("myCustomerProperty", String.class); String inheritedValueJcrTitle = ivm.getInherited("jcr:title", String.class); %> inheritedValueMyCustomProperty = <%= inheritedValueMyCustomProperty %> inheritedValueJcrTitle = <%= inheritedValueJcrTitle %> |
3. inheritedPageProperties with OSGI Bundle, Java Backend
Similar to the JSP implementation above, we can make use of the HierarchyNodeInheritanceValueMap.class, and currentPage resource to initialise the InheritanceValueMap.class object. Next, calling the method getInherited(String name, Class<T> type), we are able to get the inherited page property.
1 2 3 4 5 6 7 8 9 | import com.day.cq.commons.inherit.InheritanceValueMap; import com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap; @ScriptVariable private Page currentPage; ... InheritanceValueMap ivm = new HierarchyNodeInheritanceValueMap(currentPage.getContentResource()); String inheritedValueMyCustomProperty = ivm.getInherited("myCustomerProperty", String.class); String inheritedValueJcrTitle = ivm.getInherited("jcr:title", String.class); |
A++