Pass value from JS to sly attribute

Avatar

Avatar

v1101

Avatar

v1101

v1101

20-08-2020

Hello Community - We are using HTL Use-API to get the cookie value from the script and printing the value in the component. For some reason, it is not working as expected and it retains the previous selected value. AEM document says "Use-API" could be slower.

 

Is there any way to use the return value from a java-script to the data-sly-test attribute?

 

<sly data-sly-use.productValue="test.js" data-sly-test="${productValue.resultsFound}">
<div>${productValue.results}</span></div>
</sly>

View Entire Topic

Avatar

Avatar

Andrei_Dantsou

Avatar

Andrei_Dantsou

Andrei_Dantsou

21-08-2020

Hi @v1101,

Basically, the code written using JavaScript Use-Api is converted to Java (please see https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino for reference) and thus it is executed at a server side as well as Sightly (please see accepted solution here https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/sightly-javascript-use-imp...). If CDN and/or Dispatcher are used for caching content (the most likely), the server side approach for displaying cookies is not suitable - after html of a rendered page is cached, the back-end on Publish won't be hit to get the actual value and the cached one will be displayed.

I'd recommend following the fully front-end approach here - to move retrieval of cookies to JS stored in a clientlib. After the value is retrieved, it can be appended to necessary html element as described here https://api.jquery.com/append/

Regards