Skip to content

Instantly share code, notes, and snippets.

@gabrielwalt
Last active October 1, 2020 12:20
Show Gist options
  • Save gabrielwalt/c835d70e7263f5d0699d to your computer and use it in GitHub Desktop.
Save gabrielwalt/c835d70e7263f5d0699d to your computer and use it in GitHub Desktop.
Output JSON into markup with Sightly
use(function () {
var myObj = {
foo: "bar",
arr: [1,2,3,4,5,6,7,8,9,10]
};
return {
json: JSON.stringify(myObj)
};
});
<div data-sly-use.logic="logic.js"
data-json="${logic.json}">
...
</div>
@gabrielwalt
Copy link
Author

gabrielwalt commented Jun 30, 2015

I often see inline script elements used to output JSON, but a more elegant way of doing this is to use a data attribute instead. Also, instead of using the template language to create properly formatted JSON, using a JSON API instead is more meaningful and also much safer.

The code above can be executed with the REPL live Sightly evaluation environment:
https://github.com/Adobe-Marketing-Cloud/aem-sightly-repl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment