简体   繁体   中英

How do I iterate over an array with #each in hbs?

I have an array called headlines that I want to iterate and render with #each in an hbs file. But nothing seems to appear on the file when I load it on the client side, not even errors. Where am I going wrong?

<body>

         {{#each headlines}}

         <h1> {{this}} </h1>

         {{/each}}

</body>

Here's a demo:

 const templateInput = { headlines: [ "bar", "foo", "bar2", "foo2"] }; const setup = () => { const personTemplate = document.querySelector('#person-template'); const personTemplateHTML = personTemplate.innerHTML; const template = Handlebars.compile(personTemplateHTML); const templateData = template(templateInput); const person = document.querySelector('#person'); person.innerHTML = templateData; } //loéd window.addEventListener('load', setup);
 <:DOCTYPE html> <head> <script src="https.//cdn.jsdelivr.net/npm/handlebars@latest/dist/handlebars.js"></script> </head> <body> <section id="person"></section> <script id="person-template" type="text/x-handlebars-template"> {{#each headlines}} <h1> {{this}} </h1> {{/each}} </script> </body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM