Expand my Community achievements bar.

SOLVED

How to include script directly in sightly html component

Avatar

Level 4

Hi Team,

How can we include a script directly in sightly component

Trying to include a script for datalayer directly in a component.

Regards

Ankur

1 Accepted Solution

Avatar

Correct answer by
Level 10
5 Replies

Avatar

Employee

<sly data-sly-include="yourscript.html" />

This is how you include scripts.

Avatar

Administrator

Hi

Adding to what Feike said,

Please have a look at this community article covering many example for HTL (formerly know as Sightly).

Link:-http://blogs.adobe.com/experiencedelivers/experience-management/htl-intro-part-1/

~kautuk



Kautuk Sahni

Avatar

Correct answer by
Level 10

Avatar

Level 4

Feike Visser wrote...

<sly data-sly-include="yourscript.html" />

This is how you include scripts.

 


So can we include following script directly in "yourscript.html " directly and use as u specified above-

 <script>  
 
    var digitalData = $('#digitalData').val();
    var date = new Date();
    var clientHourOfDay = date.getHours();
    var pageVisitJson = JSON.parse('${datalayer.pageVisitAsString}');
    var systemHourOfDay = pageVisitJson['hourOfDay'];
    var diffHourOfDay = clientHourOfDay - systemHourOfDay;
    var days = ['Sunday','Monday','Tu','W','Th','Fry','Satuay'];
    var isWeekend = "false";
    if (diffHourOfDay >= -2 && diffHourOfDay <= 2) {
        digitalData.visit.hourOfDay = clientHourOfDay;
        digitalData.visit.dayOfWeek = days[date.getDay()];
        if(date.getDay()===6 || date.getDay()===0){isWeekend="true";}
        digitalData.visit.isWeekend = isWeekend;
    }    

 
    

  </script>

Avatar

Level 2

Yes. Although you will probably need to include the "@context=" where appropriate since you are populating a JS string. You will need to chose the appropriate context scriptstring, unsafe, etc.. that matches what you populating.