Expand my Community achievements bar.

SOLVED

Sightly - Issue while passing parameters to Java Use-Api

Avatar

Level 4

Hi,

 Issue while passing parameters to Java Use-Api

I tried with both the approach (Passing parameters to a use-class upon initialization and Passing parameters from data-sly-template) as mentioned on http://docs.adobe.com/docs/en/aem/6-0/develop/sightly/use-api-in-java.html

In both the cases i am not getting any result.

But If I access the java use api without passing parameters as like <div data-sly-use.info="Info"> its working fine.

Code for reference:

Info.java

package apps.testsightly.components.content.sightlyComponent;

import com.adobe.cq.sightly.WCMUse;

public class Info extends WCMUse {
    private String lowerCaseTitle;
    private String lowerCaseDescription;

    private String reverseText;
  
    @Override
    public void activate() throws Exception {
        lowerCaseTitle = "lowercaseTitle";
        lowerCaseDescription = "lowerCaseDescription";

        String text = get("text", String.class);
        reverseText = new StringBuffer(text).reverse().toString();

 

    }
  
    public String getLowerCaseTitle() {
        return lowerCaseTitle;
    }
  
    public String getLowerCaseDescription() {
        return lowerCaseDescription;
    }

    public String getReverseText()
    {

   return reverseText; 
    }
}

sightlyComponent.html

Test Sightly

 

Without Parameter

<div data-sly-use.info="Info">

  <h1>${info.lowerCaseTitle}</h1>
  <p>${info.lowerCaseDescription}</p>
     

</div>

With parameter    

    <div data-sly-use.info1="${Info @ text='Some text'}">

  <h1>${info1.lowerCaseTitle}</h1>
  <p>${info1.lowerCaseDescription}</p>
  <p>${info1.reverseText}</p>
     

</div>    

Withparameter Using Template

<div data-sly-use.info2="Info"
     data-sly-use.extra="extra.html">
     
  <h1>${info2.lowerCaseTitle}</h1>
  <p>${info2.lowerCaseDescription}</p>
     
  <div data-sly-call="${extra @ text=properties.testfield}"></div>
 
</div>

 

Extra.html

<template data-sly-template.extra="${@ text}"
          data-sly-use.extraHelper="${ExtraHelper @ text=text}">
  <p>${extraHelper.reversedText}</p>
</template>

Extra Helper.java

package apps.testsightly.components.content.sightlyComponent;

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

import com.adobe.cq.sightly.WCMUse;

package apps.my_example.components.info;
  
import com.adobe.cq.sightly.WCMUse;
  
public class ExtraHelper extends WCMUse {
    private String reversedText;

     
    @Override
    public void activate() throws Exception {
        String text = get("text", String.class);      
        reversedText = new StringBuilder(text).reverse().toString();


    }
  
    public String getReversedText() {
        return reversedText;
    }
}

 

Output:

lowercaseTitle

lowerCaseDescription

With parameter

 

 

 

Withparameter Using Template

lowercaseTitle

lowerCaseDescription

Thanks,

Karthi

1 Accepted Solution

Avatar

Correct answer by
Employee

Use it in this way...

data-sly-use.extraHelper="${'ExtraHelper' @ text=text}">

 

mind the quotes around the classname.

Can you check this?

View solution in original post

2 Replies

Avatar

Correct answer by
Employee

Use it in this way...

data-sly-use.extraHelper="${'ExtraHelper' @ text=text}">

 

mind the quotes around the classname.

Can you check this?

Avatar

Employee

The corresponding documentation page has been updated.
Thanks for having brought this to our attention!