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;
}
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
add a comment |
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
Can you include the relevantjavascript
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 withservicio_API.getForums()
is this some other third party/plugin you are using? Also I don't see any element with the id ofdivForumsList
So thejavascript
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
add a comment |
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
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
javascript handlebars.js
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 relevantjavascript
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 withservicio_API.getForums()
is this some other third party/plugin you are using? Also I don't see any element with the id ofdivForumsList
So thejavascript
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
add a comment |
Can you include the relevantjavascript
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 withservicio_API.getForums()
is this some other third party/plugin you are using? Also I don't see any element with the id ofdivForumsList
So thejavascript
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
add a comment |
2 Answers
2
active
oldest
votes
Try passing an object with the field data
to the compiled template.
var forumListCompiled = compile(forumListTemplate);
var forumListHTML = forumListCompiled({data: data});
return forumListHTML;
add a comment |
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));
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Try passing an object with the field data
to the compiled template.
var forumListCompiled = compile(forumListTemplate);
var forumListHTML = forumListCompiled({data: data});
return forumListHTML;
add a comment |
Try passing an object with the field data
to the compiled template.
var forumListCompiled = compile(forumListTemplate);
var forumListHTML = forumListCompiled({data: data});
return forumListHTML;
add a comment |
Try passing an object with the field data
to the compiled template.
var forumListCompiled = compile(forumListTemplate);
var forumListHTML = forumListCompiled({data: data});
return forumListHTML;
Try passing an object with the field data
to the compiled template.
var forumListCompiled = compile(forumListTemplate);
var forumListHTML = forumListCompiled({data: data});
return forumListHTML;
answered Nov 25 '18 at 0:55
ztadic91ztadic91
2,0651816
2,0651816
add a comment |
add a comment |
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));
add a comment |
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));
add a comment |
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));
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));
answered Nov 25 '18 at 1:21
Matt MorganMatt Morgan
2,4392821
2,4392821
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 ofdivForumsList
So thejavascript
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