Load a sightly condition based on a cookie check

Avatar

Avatar
Validate 1
Level 1
Sesha_Sai_Samud
Level 1

Like

1 like

Total Posts

3 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 1
View profile

Avatar
Validate 1
Level 1
Sesha_Sai_Samud
Level 1

Like

1 like

Total Posts

3 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 1
View profile
Sesha_Sai_Samud
Level 1

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)

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,442 likes

Total Posts

3,318 posts

Correct reply

941 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,442 likes

Total Posts

3,318 posts

Correct reply

941 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
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)

Avatar

Avatar
Give Back 5
Level 4
joeb21688728
Level 4

Likes

33 likes

Total Posts

60 posts

Correct reply

8 solutions
Top badges earned
Give Back 5
Give Back 3
Give Back 25
Give Back 10
Give Back
View profile

Avatar
Give Back 5
Level 4
joeb21688728
Level 4

Likes

33 likes

Total Posts

60 posts

Correct reply

8 solutions
Top badges earned
Give Back 5
Give Back 3
Give Back 25
Give Back 10
Give Back
View profile
joeb21688728
Level 4

09-06-2019

This is very easy. Use Sling Model and use Java logic to check a cookie. Setting and Reading Cookies : Cookie « Servlets « Java

Then set a field in the Sling Model based on the value.

Avatar

Avatar
Validate 1
Level 1
Sesha_Sai_Samud
Level 1

Like

1 like

Total Posts

3 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 1
View profile

Avatar
Validate 1
Level 1
Sesha_Sai_Samud
Level 1

Like

1 like

Total Posts

3 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 1
View profile
Sesha_Sai_Samud
Level 1

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.