March 30, 2021
Estimated Post Reading Time ~

Get Child Properties of a Node in HTL

I'm expanding this Stack Overflow article to support for ANY child node. Like Gabriel implies, you can / should make this a global class that you can call anywhere.

Logic.java

package apps.YOURMOM.components.content.common.column;

import com.adobe.cq.sightly.WCMUse;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.api.resource.ValueMap;

public class Logic extends WCMUse {

    private ValueMap childProperties;

    @Override

    public void activate() throws Exception {

        String child = get("child", String.class);

        Resource childResource = getResource().getChild(child);

        childProperties = childResource.adaptTo(ValueMap.class);

    }

    public ValueMap getChild() {

        return childProperties;

    }

}

component.html

<div data-sly-use.logic="${'Logic' @ child='col1'}" 

data-sly-test.col1="${logic.Child}"  

data-sly-resource="${ @path='col1/column-par', resourceType='wcm/foundation/components/parsys'}"

class="${[col1.mobileWidth, col1.tabletportraitWidth, col1.tabletlandscapeWidth, col1.desktopWidth, col1.columnClass] @ join=' ', context='styleToken'}" 

id="${col1.columnId @ context='styleToken'}"></div>

  1. We pass in a variable called "col1" to our Logic class.
  2. We test to see if the node exists and assign it a variable ("col1")
  3. We then use that variable to grab our style values.



By aem4beginner

No comments:

Post a Comment

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