I have a file called "list.json" set up like this:
{
"thing1": "Thing1",
"thing2": "Thing2",
"thing3": "Thing3"
}
How can I loop through this? I want to do something like:
{% for item in list%}
<option>{{ thing }}</option>
{% endfor %}
You can try following
{% for key, item in list%}
<option>{{ item }}</option>
{% endfor %}
Have you imported the JSON? If not, in the render JS, add a variable:
list: JSON.parse(fs.readFileSync('list.json'))
If you are using it multiple times, add a variable at the top of the file instead.
var LIST = JSON.parse(fs.readFileSync('list.json'))
It's also possible to use the asynchronous method, but you need some nesting:
fs.readFile('list.json', function(err, list) {
env.render('template.html', {
list: list,
//other data
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With