Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

[AEM Gems Webinar] Accelerating Experience Manager as a Cloud Service development with Rapid Development Environments
SOLVED

Passing variable value from sightly to sling model

Avatar

Level 2

I want pass list index value to sling model.

<sly data-sly-list.img="${guide.itemsList}">
<span>index:  ${imgList.index}</span>

<sly data-sly-use.multi="${'com.mycom.....MultiField' @ index=${imgList.index}' " />

This gives me null value at model. I tried to with pass with quotes , it gets passed as string.

 

Can any one help me to resolve this?

 

 

 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

6.5
1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @LaMind3 

After debugging able to find root cause for this issue, its because of datatype mismatch & datatype of itemList.index value is long. please use below updated code where datatype is changed from int to long, value will be injected as expected & it will resolve the issue.

 

Sightly:

<sly data-sly-use.multi="${'com.mycom.....MultiField' @ index = imgList.index}" />

 

 

Model Class:

@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class MultiField{

 

@Inject
private long index;

 

}

 

- Manjunath

View solution in original post

11 Replies

Avatar

Community Advisor

Hi @LaMind3 

Please find below mentioned corrected syntax & try with this.

 

Sightly:

<sly data-sly-use.multi="${'com.mycom.....MultiField' @ index = imgList.index ,context='number'}" />

 

 

Model Class:

@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class MultiField{

 

@Inject
private int index;

 

}

Avatar

Community Advisor

@LaMind3 

Try above highlighted updated code, add context='number'  & change datatype from String to int. 

Avatar

Level 2
@Manjunath_K, I tried with int in model , but it always passes 0 instead of dynamic value. I added ,context = "number" but this didnt help me ..

Avatar

Community Advisor

@LaMind3 

I verified this code in my local instance & it works fine. please find below debug screenshot where index variable injected with value '2'.

 

i will suggest you to cross check whether your model class is SlingHttpServletRequest adaptable, if 'yes' then check printing index value(which you are passing to model class) in p tag to make sure proper value is sent to model class.

 

index.PNG

 

Sightly:

<sly data-sly-use.multi="${'com.mycom.....MultiField' @ index = 2,context='number'}" />

 

 

Model Class:

@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class MultiField{

 

@Inject
private int index;

 

}

Avatar

Community Advisor

Hi @arunpatidar 

Using @Inject also we can access request attribute. i think @LaMind3 is missing to add SlingHttpServletRequest in adaptables so its not injecting the value.

 

@Inject
private int index;

Avatar

Level 2
@Manjunath_K, my model class is SlingHttpServletRequest adaptable, when I pass @ index = 2 it works, but when I pass imgList.index it does not take dynamic value. It always gives 0 when I make it as int, null when it is String

Avatar

Correct answer by
Community Advisor

Hi @LaMind3 

After debugging able to find root cause for this issue, its because of datatype mismatch & datatype of itemList.index value is long. please use below updated code where datatype is changed from int to long, value will be injected as expected & it will resolve the issue.

 

Sightly:

<sly data-sly-use.multi="${'com.mycom.....MultiField' @ index = imgList.index}" />

 

 

Model Class:

@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class MultiField{

 

@Inject
private long index;

 

}

 

- Manjunath