May 9, 2020
Estimated Post Reading Time ~

How to get node properties and populate in page using Sightly API

Create a component with the logic of

<sly data-sly-test=”${!properties.ccNodePath}” data-sly-unwrap><h5>Populating the data </h5></sly>
<sly data-sly-test=”${properties.cNodePath}” data-sly-unwrap>
<sly data-sly-use.popData=”${‘com.deo.nne.services.PopulateData’ @ nodePath = properties.cNodePath}” data-sly-unwrap></sly>
<table>
<tr class=”header”>
<td>Node Path </td>
<td>${properties.cNodePath}</td>
</tr>
<sly data-sly-list.keywordProp=”${popData.fetchNodeProps}”>
<tr>
<td>${keywordProp.text}</td>
<td>${keywordProp.value}</td>
</tr>
</sly>
</table>
</br>
</sly>
And Bundle logic

@SuppressWarnings(“deprecation”)
public class PopulateData extends WCMUse {

protected final Logger log = LoggerFactory.getLogger(this.getClass());
Session session;
List<nodeValueMap> dataResourceList = new ArrayList<nodeValueMap>();
nodeValueMap nodeValueMap;
nodeValueMapResource nodeValueMapResource;
@Override
public void activate() throws Exception {
Node newNode = null;
try{
session = getResourceResolver().adaptTo(Session.class);
String nodePath = (String) getRequest().getAttribute(genConstants.NODE_PATH); //”nodePath from component”
newNode = session.getNode(nodePath);
createProperties(newNode);
}catch(Exception e){
log.error(e.getMessage());
}
}
private void createProperties(Node newNode) {
String newChildName, newChildValue;
try{
if (newNode != null) {
PropertyIterator pt = newNode.getProperties();
while (pt.hasNext()) {
Property child = pt.nextProperty();
newChildName = child.getName();
newChildValue = child.getString();
if(!newChildName.equals(genConstants.JCR_PRIMARY_TYPE)){
nodeValueMap = new nodeValueMapDecorator(new HashMap<String, Object>());
nodeValueMap.put(genConstants.TEXT, newChildName);
nodeValueMap.put(genConstants.VALUE, newChildValue);
dataResourceList.add(nodeValueMap);
}
}
}
}catch(Exception e){
log.error(e.getMessage());
}
}
public List<nodeValueMap> fetchNodeProps(){
return dataResourceList;
}
}


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.