Load a sightly condition based on a cookie check

Sesha_Sai_Samud

06-06-2019

Hello Community,
Wanted to understand if there is a possibility in AEM to check a cookie and load a sightly tag.

My use case

<sly data-sly-test="${properties.something == 'true'}">

<script>
Cookie condition check only if true execute the below

<div data-sly-test="${properties.something }" data-sly-unwrap>

            <script type="text/javascript"  src="${properties.script}"></script>

        </div>

</script >

</sly>

Accepted Solutions (1)

Accepted Solutions (1)

Arun_Patidar

MVP

09-06-2019

I am not sure if you can do it from front end because the sightly code. But you can do it with Server side JS USE API

-------- script.js -------------

// Server-side JavaScript to get Coookie

use(function () {

    var cookie = request.getCookie("wcmmode");

    var cookieFound=false;

    if(cookie!=null && cookie.value=="edit"){

cookieFound = true;

    }

    return {

        cookieVal: cookie.value,

        cookieFound:cookieFound

    };

});

-------- Sightly code --------------

<sly data-sly-use.cObj="script.js" data-sly-test="${cObj.cookieFound }">

     ${cObj.cookieVal}

</sly>

Answers (2)

Answers (2)

Sesha_Sai_Samud

09-06-2019

Thanks Joeb,
I understand we could do a condition from backend , but we are using CDN for caching our pages . So whatever is cached first that experience would be shown, that is the reason we are looking for a front end solution.