Handlebars HBS Express - How to iterate an object without specify the properties - handlebars.js

I am trying to iterate using the properties of an object to dynamically print a table having an array with the properties and an object with the values ​​of each property.
I don't know how to do the 2 iterations using hbs express from handlebars
people: [{
name: "ken",
lastname: "grace",
age: 10
},
{
name: "ron",
lastname: "bond",
age: 20
}];
properties = ["name","lastname", "age"];
HTML CODE:
<table>
<thead>
<tr>
{{#each properties as |property index|}}
<th>
<span>{{property}}</span>
</th>
{{/each}}
</tr>
</thead>
<tbody>
{{#each people}}
<tr>
{{#each properties}}
<th>
{{!-- trying something like: --}}
{{!-- {{people.property}} --}}
</th>
{{/each}}
</tr>
{{/each}}
</tbody>
</table>

As user #76484 mentioned, you want to use the built-in lookup helper:
The lookup helper allows for dynamic parameter resolution using Handlebars variables. It can be used to look up properties of object based on data from the input.
In your specific example, you'd probably want to store your people and properties iterations in a block parameter (e.g., named |person| and |property|), as well as using ../ on your inner loop since the context has changed.
Putting that all together for you example, the HBS markup might look like:
<table>
<thead>
<tr>
{{#each properties as |property index|}}
<th><span>{{property}}</span></th>
{{/each}}
</tr>
</thead>
<tbody>
{{#each people as |person|}}
<tr>
{{#each ../properties as |property|}}
<th>{{lookup person property}}</th>
{{/each}}
</tr>
{{/each}}
</tbody>
</table>
See this playground link for the resulting HTML as well.

Related

How should i use if else and for loop with .hbs file (Handlebars.js)?

I know how can i use if else statement or for loop using .ejs file but i need to change code in .hbs file and i am new with this.Please help me with below example in which i have used .ejs file i need to convert it in .hbs file but don't know how to change if else and for loop
<!DOCTYPE html>
<html lang="en">
<head>
<title>Fetch using MySQL and Node.js</title>
</head>
<body>
<div class="table-data">
<h2>Display Data using Node.js & MySQL</h2>
<table border="1">
<tr>
<th>S.N</th>
<th>Full Name</th>
<th>Email Address</th>
<th>City</th>
<th>Country</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<%
if(userData.length!=0){
var i=1;
userData.forEach(function(data){
%>
<tr>
<td><%=i; %></td>
<td><%=data.fullName %></td>
<td><%=data.emailAddress %></td>
<td><%=data.city %></td>
<td><%=data.country %></td>
<td>Edit</td>
<td>Delete</td>
</tr>
<% i++; }) %>
<% } else{ %>
<tr>
<td colspan="7">No Data Found</td>
</tr>
<% } %>
</table>
</div>
</body>
</html>
I have used below code but it is not working as i am new i need your guidence
#76484 i have used this ``` {{#if userData.length != 0 }}
{{var i =1;}}
{{userData.forEach(function(data))}}
<tr>
<td>{{=i;}}</td>
<td>{{= data.fullName}}</td>
<td>{{= data.emailAddress}}</td>
<td>{{= data.city}}</td>
<td>{{= data.country}}</td>
<td>{{= data.Dimension_4_Score}}</td>
<td>{{= data.Total_Score_Persentage}}</td>
</tr>
{{i++; })}}
{{else{} }
<tr>
<td colspan="7">No Data Found</td>
</tr>
{{}}}
</table>
</div>
</body> ``` but it is not working
The primary difference between your embedded JS example and Handlebars is that Handlebars does not execute arbitrary JavaScript, like your .forEach loop. Instead, Handlebars provides helpers to allow you to do things like conditionals and iteration.
First, we will tackle your condition, if (userData.length != 0). Handlebars has a #if helper which we could use to check if userData has a truth (greater than 0) length. The result would be:
{{#if userData.length}}
{{! TODO: output each user data}}
{{else}}
<tr>
<td colspan="7">No Data Found</td>
</tr>
{{/if}}
Secondly, Handlebars has an #each helper which is used for looping over collections as you are doing with your userData.forEach(function(data) { /*...*/ } code. For your purposes, the syntax would be:
{{#each userData}}
<tr>
<td>{{ #index }}</td>
<td>{{ fullName }}</td>
<td>{{ emailAddress }}</td>
<td>{{ city }}</td>
<td>{{ country }}</td>
<td>Edit</td>
<td>Delete</td>
</tr>
{{/each}}
Notice how we are evaluating the properties of each object in our userData array. There is no =. We just wrap the property name in double-handlebars, like {{ fullName }}. Handlebars handles the execution context within the #each so that we are always referring to the current iteration of our array.
Also notice the {{ #index }}. This is a special variable provided by Handlebars to give us the current iteration index within our #each loop. It is zero-index, so our output will be slightly different from your ejs example because you initialized your counter at 1.
Unfortunately, if we want our indexes to be one-based, we will have to write a custom helper to this. Our helper would just need to take a number, #index, and increment it by 1. It would look like:
Handlebars.registerHelper('increment', function (num) {
return num + 1;
});
And we would update our template to make use of it:
{{increment #index }}
I have created a fiddle with the final example for your reference.

meteor spacebars nested each property by child property name

In Spacebars, can I access an outer-each’s property by name of an inner each property. I.e. ? access ymBStocks.price via via {{../{{title}}}}
More complete example
<template name="ymbStockstable">
<table class="table table-hover table-ymbStocks">
<thead>
<tr>
{{#each columns}}
<th>{{title}}</th>
{{/each}}
</tr>
</thead>
<tbody>
{{#each ymbStocks}}
<tr>
{{#each columns}}
<td>{{../columns.title}}</td>
{{/each}}
</tr>
{{/each}}
</tbody>
</table>
</template>
w3jimmy #w3jimmy 14:09
create a helper to get the property of an object
Template.yourTemplate.helpers({
getProperty: function (obj_name, prop_name){
if (obj_name.hasOwnProperty(prop_name)){
return obj_name.prop_name;
}
}
})
and then in spacebars you do like this:
{{#each ymbStock in ymbStocks}}
...
{{#each column in columns}}
<td>price: {{getProperty column.title ymbStock.price}}</td>
{{/each}}
{{/each}}`
I just spit it out, without testing...

When iterating with {{#each}} how to not include the entry in Meteor

The person I'm designing this app for requested that she be able to make a email list of people with birthdays within the next 7 days. One of the fields in the collection is Bdate in the format 'YYYY-MM-DD'. I decided to make a registerHelper with a simple algorithm that determines if the birthdate is one that fit the request:
Template.registerHelper('calculateBirthday', function(bdate) {
var birthDate = new Date(bdate);
var current = new Date();
var diff = current - birthDate; // Difference in milliseconds
var sevenDayDiff = Math.ceil(diff/31557600000) - (diff/31557600000);
if (sevenDayDiff <= 0.01995183087435)
return date;
else
return false;
});
The template would have a table that lists the birthdates that are the ones to get for the email list:
<table class="bordered">
<thead>
<tr>
<th>Name</th>
<th>Birthday</th>
</tr>
</thead>
<tbody>
{{#each QueryBirthday}}
<tr>
<tr>{{FullName}}</tr>
<td>{{calculateBirthday Bdate}}</td>
</tr>
{{/each}}
</tbody>
</table>
The problem with this is that it prints all the names with mostly blank birthdates. The algorithm works fine, but how to tell Meteor to only include those names and birthdates that 'should' be on the list?
The quickest way to hide unwanted items is
<table class="bordered">
<thead>
<tr>
<th>Name</th>
<th>Birthday</th>
</tr>
</thead>
<tbody>
{{#each QueryBirthday}}
{{#if calculateBirthday Bdate}}
<tr>
<td>{{FullName}}</td>
<td>{{calculateBirthday Bdate}}</td>
</tr>
{{/if}}
{{/each}}
</tbody>
</table>
I don't know how your application works, but like other people who commented on your question, I would filter and send only the required results from server to client.

Auto-print the fields of a Meteor collection using helpers without specifying their name

Is it possible to auto-print the fields of a Meteor collection using helpers without specifying them?
Let's say I start having a helper that returns the collection of objects stored in a table, as follows:
{{ #each CollectionData }}
<thead>
<tr>
<th>Code</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<Tr class="object-row">
<Td> {{code}} </ td>
<Td> {{description}} </ td>
</tr>
</tbody>
...
{{/each}}
Now i specify an "object schema" for each collection to set which field i want to auto-print, pseudo example:
// Items is the name of the possible collection
Schema.items var = {
fields {
code: {
columnName: "code",
show: false,
},
description: {
columnName: "description",
show: false,
},
otherField {
columnName: "foo",
show: false,
}
}
}
Now, I would make the helper to auto-generate the table columns and values ​​of a collection field where the show check is true, without having to manually specify {{code}}, {{description}} and so on, pseudo example:
{{ #each CollectionData }}
<thead>
<tr>
{{print each column where show check is == true, without manually specifing any name}}
</tr>
</thead>
<tbody>
<Tr class="object-row">
{{print the value of the column, for this record, where show check is == true, without specifing its name}}
</tr>
</tbody>
...
{{/each}}
Is there any way to do that?
The simpliest way would be to create a template for each TD, something like
<thead>
<tr>
{{#each fetchColumnHeaders}}
{{> columnHeader }}
{{/each}}
</tr>
</thead>
{{ #each CollectionData }}
<tbody>
<Tr class="object-row">
{{#each fetchColumnItems}}
{{> columnItem}}
{{/each}}
</tr>
</tbody>
{{/each}}
<template name="columnHeader">
<th>{{label}}</th>
</template>
<template name="columnItem">
<td>{{label}}</td>
</template>
And then you can write template helpers to return the column headers, and the various items based on your schema

Handlebar doesn't render a collection field correctly

I'm having problem trying to use a collection field named created in a template. Is that a reserved word, or something?
The part of the template that's struggling looks like this:
{{#each threads}}
<tr>
<td>{{topic}}</td>
<td>{{creator.username}}</td>
<!-- The line below is the evil one. -->
<td>{{created}}</td>
<td>{{lastPost.poster.username}} {{datetime lastPost.posted}}</td>
</tr>
{{/each}}
The threads I find in my console in my browser are the following:
[
Object
_id: "ngEtonq8XM36KtG3S"
created: 1375881336372
creator: function (){
creatorId: "ZmKpMdhP4GtzQo98e"
lastPost: function (){
posts: function (){
subCategory: function (){
subCategoryId: "axgd2xzctkfmphmwM"
topic: "Testing"
__proto__: Object
,
Object
_id: "XafEMvAvcRzpBKxG3"
created: 1375882602652
creator: function (){
creatorId: "ZmKpMdhP4GtzQo98e"
lastPost: function (){
posts: function (){
subCategory: function (){
subCategoryId: "axgd2xzctkfmphmwM"
topic: "Testnign again"
__proto__: Object
,
Object
_id: "CZmf5MfqZrB28SLPB"
created: 1375883440242
creator: function (){
creatorId: "ZmKpMdhP4GtzQo98e"
lastPost: function (){
posts: function (){
subCategory: function (){
subCategoryId: "axgd2xzctkfmphmwM"
topic: "And another shoot"
__proto__: Object
]
Obviously three threads, and they all have a created field. But the browser shows the following:
<tr>
<td>And another shoot</td>
<td>Peppe L-G</td>
<td></td>
<td>Peppe L-G 7 August 2013 15:50</td>
</tr>
<tr>
<td>Testnign again</td>
<td>Peppe L-G</td>
<td></td>
<td>Peppe L-G 7 August 2013 15:36</td>
</tr>
<tr>
<td>Testing</td>
<td>Peppe L-G</td>
<td></td>
<td>Peppe L-G 7 August 2013 15:35</td>
</tr>
Why isn't the created field showed?
I tried to use <td>{{this.created}}</td> instead of <td>{{created}}</td>, and then the browser shows the following:
<tr>
<td>And another shoot</td>
<td>Peppe L-G</td>
<td>1375883440242</td>
<td>Peppe L-G 7 August 2013 15:50</td>
</tr>
<tr>
<td>Testnign again</td>
<td>Peppe L-G</td>
<td></td>
<td>Peppe L-G 7 August 2013 15:36</td>
</tr>
<tr>
<td>Testing</td>
<td>Peppe L-G</td>
<td></td>
<td>Peppe L-G 7 August 2013 15:35</td>
</tr>
The created field now works for the first thread, but not the rest. What's going on?!
In case it's relevant, here's the entire template:
<template name="pageForumSubCategory">
<div class="layoutContentForumSubCategory">
{{#with subCategory}}
<h1>
Forum
→
{{name}}
{{#if currentUser}}
→
New thread
{{/if}}
</h1>
{{#if threads.count}}
<table border="2">
<thead>
<tr>
<th>Topic</th>
<th>Creator</th>
<th>Created</th>
<th>Last post</th>
</tr>
</thead>
<tbody>
{{#each threads}}
<tr>
<td>{{topic}}</td>
<td>{{creator.username}}</td>
<!-- The line below is the eveil one. -->
<td>{{this.created}}</td>
<td>{{lastPost.poster.username}} {{datetime lastPost.posted}}</td>
</tr>
{{/each}}
</tbody>
</table>
{{else}}
<p>Well, it's more or less empty here (so far).</p>
{{/if}}
{{else}}
<h1>
Forum
→
???
</h1>
<p>Hmm... There is no subforum with the given id. Strange, but there's nothing I can do about it. Sorry.</p>
{{/with}}
</div>
</template>
All templates already have a created property, which is callback (run when your template is created). The easiest solution is just to call the field something else like createdAt instead.

Resources