Expand my Community achievements bar.

SOLVED

sightly - loop elements over the script

Avatar

Level 3

Hi all,

I would like to generate script element in sightly as shown below and I've sightly list which gives me name & text

 

---- SIGHTLY LOOP---

<sly data-sly-list.page="${testModel.faqs}">

    name: ${page.name}

    text: ${page.text}

</sly>

-------------------------------------

EXPECTED OUTPUT:

<script type="application/ld+json">

    {

      "@context": "https://schema.org",

      "@type": "FAQPage",

      "mainEntity": [{

         {

        "@type": "Question",

        "name": "How long does it take to process a refund?",

        "acceptedAnswer": {

          "@type": "Answer",

          "text": “description goes here

        }

      }, {

        "@type": "Question",

        "name": "What is the policy for late/non-delivery of items ordered online?",

        "acceptedAnswer": {

          "@type": "Answer",

          "text": “description goes here

        }

      }]

    }

    </script>

 

 

Could you please help how can we integrate sightly list inside script ? 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@SonuR1 You can try something like:
1. Write logic in sling Model and generate JSON the way you want.

public String generateJSON(List<Page> faqs) {
JSONArray faqSchemaObject = new JSONArray();
try {
for(Page pagBean : faqs){
JSONObject faqItemObject = new JSONObject();
faqItemObject.put("@type","Question");
faqItemObject.put("name",pageBean.getName());
JSONObject answer = new JSONObject();
answer.put("@type","Answer");
answer.put("text",pageBean.getText());
faqItemObject.put("acceptedAnswer",answer);
faqSchemaObject.put(faqItemObject);
}
} catch (JSONException e) {
e.printStackTrace();
}
return faqSchemaObject.toString();
}

2. In sightly something like: 

<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": ${ modelobjet.jsonObject }
}

 Hope this should help you. 

View solution in original post

4 Replies

Avatar

Community Advisor

@SonuR1 I don't think you can do this way, as aem is strict xss enabled. What you can do is generate that json object in a sling model and just print a getmethod of sling model in sightly once.

Avatar

Correct answer by
Community Advisor

@SonuR1 You can try something like:
1. Write logic in sling Model and generate JSON the way you want.

public String generateJSON(List<Page> faqs) {
JSONArray faqSchemaObject = new JSONArray();
try {
for(Page pagBean : faqs){
JSONObject faqItemObject = new JSONObject();
faqItemObject.put("@type","Question");
faqItemObject.put("name",pageBean.getName());
JSONObject answer = new JSONObject();
answer.put("@type","Answer");
answer.put("text",pageBean.getText());
faqItemObject.put("acceptedAnswer",answer);
faqSchemaObject.put(faqItemObject);
}
} catch (JSONException e) {
e.printStackTrace();
}
return faqSchemaObject.toString();
}

2. In sightly something like: 

<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": ${ modelobjet.jsonObject }
}

 Hope this should help you. 

Avatar

Administrator

@SonuR1 We hope you found the AEM community valuable. We anticipate your return as either a learner or a contributor. The community benefits from SMEs like you. Please ask your AEM peers to join and contribute. Happy AEM learning!



Kautuk Sahni