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.

how to pass handlebar attributes in data-sly-use

Avatar

Level 2

Hi All, 

 

We are calling WCMUsePojo (AbcDetails) and want to pass an attribute (part no) which are coming via handlebar, and somehow the attributes are not getting resolved. However, in data-sly-call we can pass the handlebar attributes. 

 

<sly
data-sly-use.abcDetails="${'com.abc.demo.AbcDetails' @partNo = '{{../partNo}}'}">
<sly
data-sly-use.testTemplate="components/content/test-template.html">
 
<sly
<!--  this works - IF we can get abcDetails by passing hardcoded partNo -->
data-sly-call="${testTemplate.demoOrder @abcInfo=abcDetails, definition='{{../../demo}}', parentIndex='{{../../test}}', info='{{../test1}}'}">
</sly>
</sly>
</sly>
 
Need urgent help, can we not pass handlebar attributes to data-sly-use
5 Replies

Avatar

Community Advisor

Hello @Gunjan_13  - 

 

  • In HTL, you cannot directly pass handlebar attributes to data-sly-use directives. The data-sly-use directive expects a relative or absolute path to a Java class or a Use-API object reference.
  • However, you can achieve the desired functionality by passing the handlebar attributes to the data-sly-call block and then passing them as properties to the Use-API object within the Java class.

 

<sly data-sly-use.abcDetails="com.abc.demo.AbcDetails">
    <sly data-sly-call="${testTemplate.demoOrder @abcInfo=abcDetails.getProperties(), partNo=partNo}">
    </sly>
</sly>

 

Avatar

Level 2

Thank you @Tanika02  for looking into it. 

The problem is I need to call the WCMUse and need to pass a value. And then I will get an object from the WCMUse, and later I need to call another sightly and pass that object and few more attributes. These attributes I am getting from handlebar and the below line works too (if I can get abcDetails anyhow)

 

data-sly-call="${testTemplate.demoOrder @abcInfo=abcDetails, definition='{{../../demo}}', parentIndex='{{../../test}}', info='{{../test1}}'}">

 

NOT WORKING -

data-sly-use.abcDetails="${'com.abc.demo.AbcDetails' @partNo = '{{../partNo}}'}">

 

partNo is going AS IS  ( '{{../partNo}}') to the WCMUse, I am not sure why it is not getting resolved. But, when I save the value to the workingPartNoworkingPartNo is getting printed correctly. 

<sly data-sly-test.workingPartNo ="{{../partNo}}">

 

workingPartNo - has the right value. 

 

ISSUE - How to pass workingPartNo to WCUSe and get the abcDetails.

Avatar

Community Advisor

Hi @Gunjan_13 , when you were able to print them correctly <sly data-sly-test.workingPartNo ="{{../partNo}}"> can you try sending saved workingPartNo instead of handlebar attribute directly like below

<data-sly-use.abcDetails="${'com.abc.demo.AbcDetails' @partNo = workingPartNo}">

 

I believe handlebar values are only accessible inside handlebar script tags.

Avatar

Level 2

Hi @krishna_sai  yes was able to print it, and tried sending like above, But it dint work. 

Avatar

Community Advisor

Hello @Gunjan_13 

 

Have you tried doing this the JavaScript way?

 

 

<script data-sly-script="workingPartNo = ${../partNo @ context='scriptString'}"></script>
<sly data-sly-use.abcDetails="com.abc.demo.AbcDetails @partNo=workingPartNo"></sly>

 

 

  • In the above code, we use the <script> tag with the data-sly-script attribute to store the resolved value of ../partNo in the workingPartNo JavaScript variable. The @ context='scriptString' is used to ensure the value is treated as a string.
  • Next, we use the data-sly-use directive to instantiate the com.abc.demo.AbcDetails class, passing workingPartNo as the value for the @partNo attribute.