Passing cookie value to data-sly-use without using Use-API

Avatar

Avatar

viki1101

Avatar

viki1101

viki1101

03-09-2020

Hello Community - I wanted to use the cookie or sessionstorage value in clientside using Javascript or Jquery (Not USE-API as this will be executed in serverside) and pass the value as one of the parameter(Instead of 'xyz') in data-sly-use & data-sly-test as well. Please let me know how to achieve this in HTL without using Use-API.

<div data-sly-use.testobj="${'com.aem.test.core.models.productInfo' @ cookieVal=xyz" data-sly-unwrap>

</div>

Any help in this is highly appreciated.

View Entire Topic

Avatar

Avatar

Nupur_Jain

MVP

Avatar

Nupur_Jain

MVP

Nupur_Jain
MVP

04-09-2020

Hi @viki1101 

 

If you want to pass value from clientside to serverside, session storage can never be an option as it is clientside and can not be accessed serverside.

 

You can use cookie as the cookie values are passed to serverside via "cookie" request header. In order to get the value of cookie.  You can use use api to get cookie value and then pass it to your sling model like this

 

cookie.js 

 

use(function () {

    var cookieValue = request.getCookie("cookiename");

    return {

       cvalue : cookieValue

    };

});

 

HTL

 

<div data-sly-use.cookie="cookie.js" data-sly-use.testobj="${'com.aem.test.core.models.productInfo' @ cookieVal=cookie.cvalue" data-sly-unwrap>

</div>

 

 

Hope it helps!

Thanks

Nupur