Sightly - Issue while passing parameters to Java Use-Api

Avatar

Avatar

Premkarthic-7WP

Avatar

Premkarthic-7WP

Premkarthic-7WP

15-10-2015

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

View Entire Topic

Avatar

Avatar

gabriel_walt

Employee

Avatar

gabriel_walt

Employee

gabriel_walt
Employee

15-10-2015

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