How to "hard code" the JSON variables into a script tag when I need them in the JavaScript of a page

Following up on this question I would like to find out more in detail how to "hard code the JSON variables into a script tag" when I need to work with them in JavaScript.

For example the tags variable in the page's JSON structure, how can I access this in JavaScript? Do I read it out of the DOM , say, in the load event? Sorry, but I am a bit confused here.

2 replies

Something like this:

{{#entry}}
...
<script>
var author = "{{metadata.author}}";
alert('Author is' + author);
</script>
...
{{/entry}}

Does that make sense? You can even iterate over lists to generate arrays:

<script>
var tags = [ {{#tags}} { tag: "{{tag}}" }, {{/tags}} ];
</script>
Answered 3 months ago · Improve this answer

Thanks, David. Much appreciate your reply.

I can see this is essentially defining new variables in JS, not accessing the underlying JSON data, right?

Does this mean there is no way to _modify_the JSON data?

Answered 3 months ago · Improve this answer

Markdown allowed
Question or feedback?
Contact us

Tags

developers 28 questions bug 14 questions posts 9 questions resolved 7 questions request 6 questions google-drive 5 questions configure 4 questions pages 3 questions added 3 questions meta 3 questions More tags →
Subscribe for changes
RSS Feed of latest questions