Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

Unable to Iteare Paragraph object in sightly

Avatar

Level 4

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(HTL):

<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)

3 Replies

Avatar

Employee

Looking at your code I see the following:

Java: getParagraphs()

HTL: paragraphList

Make sure the naming is correct. I was expecting paragraphs in HTL.

Avatar

Level 10

As we discuss in many HLT articles, the name of the method in Java and HTL must match, You can drop the get so getFoo in Java becomes foo in HTL.

See: Creating an Adobe Experience Manager HTML Template Language component that uses the WCMUsePojo API