Handlebars each sentence not looping





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have this template



<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Nombre del foro</th>
</tr>
</thead>
<tbody>

{{#each data}}
<tr>
<th scope="row">{{ id }}</th>
<td>{{name}}</td>
</tr>
{{/each}}

</tbody>
</table>


with this input data



[
{"id":1,"name":"Foro general"},
{"id":2,"name":"Otro foro"},
{"id":14,"name":"Nuevo foro"},
{"id":15,"name":"Nuevo foro"},
{"id":16,"name":"Nuevo foro"},
{"id":17,"name":"Nuevo foro"},
{"id":18,"name":"Nuevo foro"}
]


and the output is this



<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Nombre del foro</th>
</tr>
</thead>
<tbody>
</tbody>
</table>


I don't know why the JSON is treated as an empty array of objects.
Any ideas what am I missing?



Edit 2: Remove confusing code that was unnecessary



forumListTemplate(data){
var forumListTemplate =
`
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Nombre del foro</th>
</tr>
</thead>
<tbody>
{{#each data}}
<tr>
<th scope="row">{{ id }}</th>
<td>{{name}}</td>
</tr>

{{/each}}
</tbody>
</table>
`
var forumListCompiled = compile(forumListTemplate);
var forumListHTML = forumListCompiled(data);
return forumListHTML;
}


I have checked that result returns the input data mentioned above










share|improve this question

























  • Can you include the relevant javascript to where you append the data?

    – NewToJS
    Nov 24 '18 at 22:49











  • Updated :) @NewToJS. I also used to test it and it's the same result tryhandlebarsjs.com

    – Ricardo Rodriguez
    Nov 24 '18 at 23:21













  • I am not familiar with servicio_API.getForums() is this some other third party/plugin you are using? Also I don't see any element with the id of divForumsList So the javascript you have added is useless if not confusing to me.

    – NewToJS
    Nov 24 '18 at 23:29













  • I'm sorry I misread what you were asking for, I've updated the question again. I think this is what you wanted me to post

    – Ricardo Rodriguez
    Nov 24 '18 at 23:49


















0















I have this template



<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Nombre del foro</th>
</tr>
</thead>
<tbody>

{{#each data}}
<tr>
<th scope="row">{{ id }}</th>
<td>{{name}}</td>
</tr>
{{/each}}

</tbody>
</table>


with this input data



[
{"id":1,"name":"Foro general"},
{"id":2,"name":"Otro foro"},
{"id":14,"name":"Nuevo foro"},
{"id":15,"name":"Nuevo foro"},
{"id":16,"name":"Nuevo foro"},
{"id":17,"name":"Nuevo foro"},
{"id":18,"name":"Nuevo foro"}
]


and the output is this



<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Nombre del foro</th>
</tr>
</thead>
<tbody>
</tbody>
</table>


I don't know why the JSON is treated as an empty array of objects.
Any ideas what am I missing?



Edit 2: Remove confusing code that was unnecessary



forumListTemplate(data){
var forumListTemplate =
`
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Nombre del foro</th>
</tr>
</thead>
<tbody>
{{#each data}}
<tr>
<th scope="row">{{ id }}</th>
<td>{{name}}</td>
</tr>

{{/each}}
</tbody>
</table>
`
var forumListCompiled = compile(forumListTemplate);
var forumListHTML = forumListCompiled(data);
return forumListHTML;
}


I have checked that result returns the input data mentioned above










share|improve this question

























  • Can you include the relevant javascript to where you append the data?

    – NewToJS
    Nov 24 '18 at 22:49











  • Updated :) @NewToJS. I also used to test it and it's the same result tryhandlebarsjs.com

    – Ricardo Rodriguez
    Nov 24 '18 at 23:21













  • I am not familiar with servicio_API.getForums() is this some other third party/plugin you are using? Also I don't see any element with the id of divForumsList So the javascript you have added is useless if not confusing to me.

    – NewToJS
    Nov 24 '18 at 23:29













  • I'm sorry I misread what you were asking for, I've updated the question again. I think this is what you wanted me to post

    – Ricardo Rodriguez
    Nov 24 '18 at 23:49














0












0








0








I have this template



<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Nombre del foro</th>
</tr>
</thead>
<tbody>

{{#each data}}
<tr>
<th scope="row">{{ id }}</th>
<td>{{name}}</td>
</tr>
{{/each}}

</tbody>
</table>


with this input data



[
{"id":1,"name":"Foro general"},
{"id":2,"name":"Otro foro"},
{"id":14,"name":"Nuevo foro"},
{"id":15,"name":"Nuevo foro"},
{"id":16,"name":"Nuevo foro"},
{"id":17,"name":"Nuevo foro"},
{"id":18,"name":"Nuevo foro"}
]


and the output is this



<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Nombre del foro</th>
</tr>
</thead>
<tbody>
</tbody>
</table>


I don't know why the JSON is treated as an empty array of objects.
Any ideas what am I missing?



Edit 2: Remove confusing code that was unnecessary



forumListTemplate(data){
var forumListTemplate =
`
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Nombre del foro</th>
</tr>
</thead>
<tbody>
{{#each data}}
<tr>
<th scope="row">{{ id }}</th>
<td>{{name}}</td>
</tr>

{{/each}}
</tbody>
</table>
`
var forumListCompiled = compile(forumListTemplate);
var forumListHTML = forumListCompiled(data);
return forumListHTML;
}


I have checked that result returns the input data mentioned above










share|improve this question
















I have this template



<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Nombre del foro</th>
</tr>
</thead>
<tbody>

{{#each data}}
<tr>
<th scope="row">{{ id }}</th>
<td>{{name}}</td>
</tr>
{{/each}}

</tbody>
</table>


with this input data



[
{"id":1,"name":"Foro general"},
{"id":2,"name":"Otro foro"},
{"id":14,"name":"Nuevo foro"},
{"id":15,"name":"Nuevo foro"},
{"id":16,"name":"Nuevo foro"},
{"id":17,"name":"Nuevo foro"},
{"id":18,"name":"Nuevo foro"}
]


and the output is this



<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Nombre del foro</th>
</tr>
</thead>
<tbody>
</tbody>
</table>


I don't know why the JSON is treated as an empty array of objects.
Any ideas what am I missing?



Edit 2: Remove confusing code that was unnecessary



forumListTemplate(data){
var forumListTemplate =
`
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Nombre del foro</th>
</tr>
</thead>
<tbody>
{{#each data}}
<tr>
<th scope="row">{{ id }}</th>
<td>{{name}}</td>
</tr>

{{/each}}
</tbody>
</table>
`
var forumListCompiled = compile(forumListTemplate);
var forumListHTML = forumListCompiled(data);
return forumListHTML;
}


I have checked that result returns the input data mentioned above







javascript handlebars.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 23:47







Ricardo Rodriguez

















asked Nov 24 '18 at 22:43









Ricardo RodriguezRicardo Rodriguez

194




194













  • Can you include the relevant javascript to where you append the data?

    – NewToJS
    Nov 24 '18 at 22:49











  • Updated :) @NewToJS. I also used to test it and it's the same result tryhandlebarsjs.com

    – Ricardo Rodriguez
    Nov 24 '18 at 23:21













  • I am not familiar with servicio_API.getForums() is this some other third party/plugin you are using? Also I don't see any element with the id of divForumsList So the javascript you have added is useless if not confusing to me.

    – NewToJS
    Nov 24 '18 at 23:29













  • I'm sorry I misread what you were asking for, I've updated the question again. I think this is what you wanted me to post

    – Ricardo Rodriguez
    Nov 24 '18 at 23:49



















  • Can you include the relevant javascript to where you append the data?

    – NewToJS
    Nov 24 '18 at 22:49











  • Updated :) @NewToJS. I also used to test it and it's the same result tryhandlebarsjs.com

    – Ricardo Rodriguez
    Nov 24 '18 at 23:21













  • I am not familiar with servicio_API.getForums() is this some other third party/plugin you are using? Also I don't see any element with the id of divForumsList So the javascript you have added is useless if not confusing to me.

    – NewToJS
    Nov 24 '18 at 23:29













  • I'm sorry I misread what you were asking for, I've updated the question again. I think this is what you wanted me to post

    – Ricardo Rodriguez
    Nov 24 '18 at 23:49

















Can you include the relevant javascript to where you append the data?

– NewToJS
Nov 24 '18 at 22:49





Can you include the relevant javascript to where you append the data?

– NewToJS
Nov 24 '18 at 22:49













Updated :) @NewToJS. I also used to test it and it's the same result tryhandlebarsjs.com

– Ricardo Rodriguez
Nov 24 '18 at 23:21







Updated :) @NewToJS. I also used to test it and it's the same result tryhandlebarsjs.com

– Ricardo Rodriguez
Nov 24 '18 at 23:21















I am not familiar with servicio_API.getForums() is this some other third party/plugin you are using? Also I don't see any element with the id of divForumsList So the javascript you have added is useless if not confusing to me.

– NewToJS
Nov 24 '18 at 23:29







I am not familiar with servicio_API.getForums() is this some other third party/plugin you are using? Also I don't see any element with the id of divForumsList So the javascript you have added is useless if not confusing to me.

– NewToJS
Nov 24 '18 at 23:29















I'm sorry I misread what you were asking for, I've updated the question again. I think this is what you wanted me to post

– Ricardo Rodriguez
Nov 24 '18 at 23:49





I'm sorry I misread what you were asking for, I've updated the question again. I think this is what you wanted me to post

– Ricardo Rodriguez
Nov 24 '18 at 23:49












2 Answers
2






active

oldest

votes


















0














Try passing an object with the field data to the compiled template.



var forumListCompiled = compile(forumListTemplate);
var forumListHTML = forumListCompiled({data: data});
return forumListHTML;





share|improve this answer































    0














    For what it's worth to you or anyone else viewing this question (maybe nothing, if you're committed to using Handlebars) you don't need a third-party library to accomplish this in modern JavaScript. You can just use template strings.



    I understand that your original question was about how to use Handlebars, but maybe this will be useful to you now or down the road. The advantage is, no need to import an outside library and increase the size of your application:






    const inputData = [
    {"id": 1, "name": "Foro general"},
    {"id": 2, "name": "Otro foro"},
    {"id": 14, "name": "Nuevo foro"},
    {"id": 15, "name": "Nuevo foro"},
    {"id": 16, "name": "Nuevo foro"},
    {"id": 17, "name": "Nuevo foro"},
    {"id": 18, "name": "Nuevo foro"}
    ]

    function forumListTemplate(data){
    const trs = data.map(entry => (
    `<tr>
    <th scope="row">${entry.id}</th>
    <td>${entry.name}</td>
    </tr>`
    )
    )

    return `<table class="table">
    <thead>
    <tr>
    <th scope="col">#</th>
    <th scope="col">Nombre del foro</th>
    </tr>
    </thead>
    <tbody>
    ${trs}
    </tbody>
    </table>`
    }

    console.log(forumListTemplate(inputData));








    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462996%2fhandlebars-each-sentence-not-looping%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Try passing an object with the field data to the compiled template.



      var forumListCompiled = compile(forumListTemplate);
      var forumListHTML = forumListCompiled({data: data});
      return forumListHTML;





      share|improve this answer




























        0














        Try passing an object with the field data to the compiled template.



        var forumListCompiled = compile(forumListTemplate);
        var forumListHTML = forumListCompiled({data: data});
        return forumListHTML;





        share|improve this answer


























          0












          0








          0







          Try passing an object with the field data to the compiled template.



          var forumListCompiled = compile(forumListTemplate);
          var forumListHTML = forumListCompiled({data: data});
          return forumListHTML;





          share|improve this answer













          Try passing an object with the field data to the compiled template.



          var forumListCompiled = compile(forumListTemplate);
          var forumListHTML = forumListCompiled({data: data});
          return forumListHTML;






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 0:55









          ztadic91ztadic91

          2,0651816




          2,0651816

























              0














              For what it's worth to you or anyone else viewing this question (maybe nothing, if you're committed to using Handlebars) you don't need a third-party library to accomplish this in modern JavaScript. You can just use template strings.



              I understand that your original question was about how to use Handlebars, but maybe this will be useful to you now or down the road. The advantage is, no need to import an outside library and increase the size of your application:






              const inputData = [
              {"id": 1, "name": "Foro general"},
              {"id": 2, "name": "Otro foro"},
              {"id": 14, "name": "Nuevo foro"},
              {"id": 15, "name": "Nuevo foro"},
              {"id": 16, "name": "Nuevo foro"},
              {"id": 17, "name": "Nuevo foro"},
              {"id": 18, "name": "Nuevo foro"}
              ]

              function forumListTemplate(data){
              const trs = data.map(entry => (
              `<tr>
              <th scope="row">${entry.id}</th>
              <td>${entry.name}</td>
              </tr>`
              )
              )

              return `<table class="table">
              <thead>
              <tr>
              <th scope="col">#</th>
              <th scope="col">Nombre del foro</th>
              </tr>
              </thead>
              <tbody>
              ${trs}
              </tbody>
              </table>`
              }

              console.log(forumListTemplate(inputData));








              share|improve this answer




























                0














                For what it's worth to you or anyone else viewing this question (maybe nothing, if you're committed to using Handlebars) you don't need a third-party library to accomplish this in modern JavaScript. You can just use template strings.



                I understand that your original question was about how to use Handlebars, but maybe this will be useful to you now or down the road. The advantage is, no need to import an outside library and increase the size of your application:






                const inputData = [
                {"id": 1, "name": "Foro general"},
                {"id": 2, "name": "Otro foro"},
                {"id": 14, "name": "Nuevo foro"},
                {"id": 15, "name": "Nuevo foro"},
                {"id": 16, "name": "Nuevo foro"},
                {"id": 17, "name": "Nuevo foro"},
                {"id": 18, "name": "Nuevo foro"}
                ]

                function forumListTemplate(data){
                const trs = data.map(entry => (
                `<tr>
                <th scope="row">${entry.id}</th>
                <td>${entry.name}</td>
                </tr>`
                )
                )

                return `<table class="table">
                <thead>
                <tr>
                <th scope="col">#</th>
                <th scope="col">Nombre del foro</th>
                </tr>
                </thead>
                <tbody>
                ${trs}
                </tbody>
                </table>`
                }

                console.log(forumListTemplate(inputData));








                share|improve this answer


























                  0












                  0








                  0







                  For what it's worth to you or anyone else viewing this question (maybe nothing, if you're committed to using Handlebars) you don't need a third-party library to accomplish this in modern JavaScript. You can just use template strings.



                  I understand that your original question was about how to use Handlebars, but maybe this will be useful to you now or down the road. The advantage is, no need to import an outside library and increase the size of your application:






                  const inputData = [
                  {"id": 1, "name": "Foro general"},
                  {"id": 2, "name": "Otro foro"},
                  {"id": 14, "name": "Nuevo foro"},
                  {"id": 15, "name": "Nuevo foro"},
                  {"id": 16, "name": "Nuevo foro"},
                  {"id": 17, "name": "Nuevo foro"},
                  {"id": 18, "name": "Nuevo foro"}
                  ]

                  function forumListTemplate(data){
                  const trs = data.map(entry => (
                  `<tr>
                  <th scope="row">${entry.id}</th>
                  <td>${entry.name}</td>
                  </tr>`
                  )
                  )

                  return `<table class="table">
                  <thead>
                  <tr>
                  <th scope="col">#</th>
                  <th scope="col">Nombre del foro</th>
                  </tr>
                  </thead>
                  <tbody>
                  ${trs}
                  </tbody>
                  </table>`
                  }

                  console.log(forumListTemplate(inputData));








                  share|improve this answer













                  For what it's worth to you or anyone else viewing this question (maybe nothing, if you're committed to using Handlebars) you don't need a third-party library to accomplish this in modern JavaScript. You can just use template strings.



                  I understand that your original question was about how to use Handlebars, but maybe this will be useful to you now or down the road. The advantage is, no need to import an outside library and increase the size of your application:






                  const inputData = [
                  {"id": 1, "name": "Foro general"},
                  {"id": 2, "name": "Otro foro"},
                  {"id": 14, "name": "Nuevo foro"},
                  {"id": 15, "name": "Nuevo foro"},
                  {"id": 16, "name": "Nuevo foro"},
                  {"id": 17, "name": "Nuevo foro"},
                  {"id": 18, "name": "Nuevo foro"}
                  ]

                  function forumListTemplate(data){
                  const trs = data.map(entry => (
                  `<tr>
                  <th scope="row">${entry.id}</th>
                  <td>${entry.name}</td>
                  </tr>`
                  )
                  )

                  return `<table class="table">
                  <thead>
                  <tr>
                  <th scope="col">#</th>
                  <th scope="col">Nombre del foro</th>
                  </tr>
                  </thead>
                  <tbody>
                  ${trs}
                  </tbody>
                  </table>`
                  }

                  console.log(forumListTemplate(inputData));








                  const inputData = [
                  {"id": 1, "name": "Foro general"},
                  {"id": 2, "name": "Otro foro"},
                  {"id": 14, "name": "Nuevo foro"},
                  {"id": 15, "name": "Nuevo foro"},
                  {"id": 16, "name": "Nuevo foro"},
                  {"id": 17, "name": "Nuevo foro"},
                  {"id": 18, "name": "Nuevo foro"}
                  ]

                  function forumListTemplate(data){
                  const trs = data.map(entry => (
                  `<tr>
                  <th scope="row">${entry.id}</th>
                  <td>${entry.name}</td>
                  </tr>`
                  )
                  )

                  return `<table class="table">
                  <thead>
                  <tr>
                  <th scope="col">#</th>
                  <th scope="col">Nombre del foro</th>
                  </tr>
                  </thead>
                  <tbody>
                  ${trs}
                  </tbody>
                  </table>`
                  }

                  console.log(forumListTemplate(inputData));





                  const inputData = [
                  {"id": 1, "name": "Foro general"},
                  {"id": 2, "name": "Otro foro"},
                  {"id": 14, "name": "Nuevo foro"},
                  {"id": 15, "name": "Nuevo foro"},
                  {"id": 16, "name": "Nuevo foro"},
                  {"id": 17, "name": "Nuevo foro"},
                  {"id": 18, "name": "Nuevo foro"}
                  ]

                  function forumListTemplate(data){
                  const trs = data.map(entry => (
                  `<tr>
                  <th scope="row">${entry.id}</th>
                  <td>${entry.name}</td>
                  </tr>`
                  )
                  )

                  return `<table class="table">
                  <thead>
                  <tr>
                  <th scope="col">#</th>
                  <th scope="col">Nombre del foro</th>
                  </tr>
                  </thead>
                  <tbody>
                  ${trs}
                  </tbody>
                  </table>`
                  }

                  console.log(forumListTemplate(inputData));






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 25 '18 at 1:21









                  Matt MorganMatt Morgan

                  2,4392821




                  2,4392821






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462996%2fhandlebars-each-sentence-not-looping%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Tangent Lines Diagram Along Smooth Curve

                      Yusuf al-Mu'taman ibn Hud

                      Zucchini