Unable to Iteare Paragraph object in sightly

632 views Asked by At

I am trying to convert the existing component code from JSP to Sightly. For that I need to get the paragarph object in the backend i.e. java and send it to sightly(HTL).

Existing code in JSP:

<%
ParagraphSystem parSys = ParagraphSystem.create(resource, slingRequest);
for (Paragraph par: parSys.paragraphs()) { %>
<sling:include resource="<%= par %>"/><%
} %>

In this way there are including a resource inside a component.

Below is the new sightly & jave code for the above one:

Java:

public class AnchorList extends WCMUsePojo{

private List<Paragraph> paragraphs;

public List<Paragraph> getParagraphs() {
    return paragraphs;
}

public void setParagraphs(List<Paragraph> paragraphs) {
    this.paragraphs = paragraphs;
}

public void activate() throws Exception{

    paragraphs = new LinkedList<Paragraph>();
    Resource resource = getResource();

    SlingHttpServletRequest slingRequest = getRequest();

    ParagraphSystem parSys = ParagraphSystem.create(resource, slingRequest);

    for (Paragraph par: parSys.paragraphs()) {
        paragraphs.add(par);
    }
}

}

Sightly:

<div data-sly-use.anchorList="com.xxx.components.AnchorList"data-sly-unwrap>
<div data-sly-list.paras="${anchorList.paragraphList}" data-sly-unwrap>

    <div data-sly-test.paraList="${paras}" data-sly-unwrap></div>
    <div class="anchorlistitem section collapsed">
    <div data-sly-resource="${@ resource=paraList}" data-sly-unwrap></div>
    </div> 
    </div>

I am facing two issues over here. First, I am unable to get the paragraph object from java to front end using sightly. I tried to validate it by displaying the values of the paragraph object using the following:

<div>
para object is ${paras}
</div>

But it was not diplaying any value. I am able to see tha paragraph object values in the java class but its unable to read that in sightly.

Second issue is, I need the equivalent sightly code for JSP to include a resource in the component. I am not sure whether the code which I have written using the data-sly-resource is correct or not.

Below is the error log I am getting when I am trying to run the above java/htl code:

19.12.2016 00:22:51.778 ERROR [0:0:0:0:0:0:0:1 [1482124971543] GET /website/en/home/what-is-totalinsight/investing.html HTTP/1.1] com.day.cq.wcm.core.impl.WCMDebugFilter Exception: org.apache.sling.api.request.RecursionTooDeepException: /content/websitet/en/home/what-is-totalinsight/investing/jcr:content/page_content/achorlist org.apache.sling.scripting.sightly.SightlyException: org.apache.sling.api.request.RecursionTooDeepException: /content/website/en/home/what-is-totalinsight/investing/jcr:content/page_content/achorlist at com.adobe.cq.sightly.WCMScriptHelper.includeResource(WCMScriptHelper.java:143) at com.adobe.cq.sightly.WCMScriptHelper.includeResource(WCMScriptHelper.java:86) at com.adobe.cq.sightly.internal.extensions.ResourceExtension.call(ResourceExtension.java:99) at org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl.call(RenderContextImpl.java:89) at apps.pnc_total_insight.components.elements.achorlist.SightlyJava_achorlist.render(SightlyJava_achorlist.java:116) at org.apache.sling.scripting.sightly.impl.engine.runtime.RenderUnit.render(RenderUnit.java:52) at org.apache.sling.scripting.sightly.impl.engine.SightlyScriptEngine.evaluateScript(SightlyScriptEngine.java:95) at org.apache.sling.scripting.sightly.impl.engine.SightlyScriptEngine.eval(SightlyScriptEngine.java:83) at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:361)

2

There are 2 answers

0
Vlad On

There are several issues with your code:

  • You defined a getter for paragraphs list as AnchorList#getParagrahs but you invoke it as anchorList.paragraphList from the HTL/Sightly script. You should instead use anchorList.paragraphs
  • The data-sly-test.paraList="${paras}" piece is unnecessary. You access the current item using the iterator variable paras, there's no need to reassign it.
  • In order to include the paragraph resource you should use the path: data-sly-resource="${paras.path @ resourceType}". In newer versions of HTL/Sightly you will be able to use the actual resource directly: https://issues.apache.org/jira/browse/SLING-5811

Depending on the version of HTL/Sightly that you are using, you might want to replace the <div data-sly-unwrap...>.. with <sly..>..

0
rakhi4110 On

You can't use the resource object directly. Instead you can include the resource using the resource path and the resource type as shown below.

<sly data-sly-use.anchorList="com.xxx.components.AnchorList">
    <sly data-sly-list.paras="${anchorList.paragraphList}">
        <div class="anchorlistitem section collapsed">
            <sly data-sly-resource="${paras.resourcePath @ resourceType=paras.resourceType}"></sly>
    </sly>
</sly>

Also, you can use <sly> tag instead of using data-sly-unwrap with each element.