handlebars each in each arguments - object

Hi I have this problem with HB.
This are my example arguments saved in a variables
var data = {
people: [ Katz", Johnson", Jolley"],
color:['blue','red','green'],
myClass:['first',second,'third']
}
<div class="people_list">
{{#each data}}
<div>{{people}}
{{#each color}}
<div class='{{ProblemHereMyClass}}'>{{this}}</div>
{{/each}}
</div>
{{/each}}
</div>
How to print key myClass. I try more & more time, follow several tips but no result.
Have an idea?

First use ../ to change the scope while looping. Second use lookup to pick an array item by #index.
<div class="people_list">
{{#each data.people}}
<div>{{this}}
{{#each ../data.color}}
<div class="{{lookup ../../data.myClass #index}}">{{this}}</div>
{{/each}}
</div>
{{/each}}
</div>

Related

Handlebars active TAB

I have a simple pagination. What I want to do is adding class "active" to active tab (I am on page 8 so button 8 has "active" class)
But it still won't work.
I tried two things but both unsuccessful.
First this:
{{#each pagination}}
{{#if pagHelper}}
<a>{{this.number}}</a>
{{else}}
<li class="paginator-item {{#is this.number current}}active{{/is}}">
{{this.number}}
</li>
{{/if}}
{{/each}}
Second like this:
{{#is this.number current}}{}
{{#each pagination}}
{{#if pagHelper}}
<a>{{this.number}}</a>
{{else}}
<li class="paginator-item ">
{{this.number}}
</li>
{{/if}}
{{/each}}
{{else}}
{{#each pagination}}
{{#if pagHelper}}
<a>{{this.number}}</a>
{{else}}
<li class="paginator-item ">
{{this.number}}
</li>
{{/if}}
{{/each}}
{{/is}}
Basically, I am comparing the current page number and the page number of the button. I tried to log both "current" and "this number" and both gave me correct value. The problem is that when I use "current" in "each" loop it gives me nothing. I will be thankfull for any idea.
I just needed to add ../ to current
{{#is this.number current}}active{{/is}}
to
{{#is this.number ../current}}active{{/is}}

Use handlebars Java to iterate over a map of maps

I am currently creating a small Ratpack, Java, Groovy project. I am using Handlebars Java as my templating system.
https://github.com/jknack/handlebars.java
I am attempting to iterate over a map that has a key and then a map as a value. This is an example of my data.
[manOfTheMatches:[Forlan:20], cleanSheets:[Rooney:30]]
I want to iterate over each item in my map and then iterate over the map within each value to display for example.
"Most man of the matches Forlan with 20"
At the moment I am doing this.
{{#each mostMotm}}
<div class="col-sm-4 col-xl-3">
<div class="panel panel-tile text-center br-a br-grey">
<div class="panel-body">
<h6 class="text-success">Most {{#key}}</h6>
</div>
<div class="panel-footer br-t p12">
<span class="fs11">
<i class="fa fa-arrow-up pr5"></i>
<b>{{this}}</b>
</span>
</div>
</div>
</div>
{{/each}}
#key correctly prints out the key however 'this' just displays Forlan:20. I tried to loop over the inside map but had no luck, has anyone come across this before?
I got around this by looping over the inside map using this below.
{{#each this}}
{{this}}
{{/each}}
my code now looks like :
{{#each mostMotm}}
<div class="col-sm-4 col-xl-3">
<div class="panel panel-tile text-center br-a br-grey">
<div class="panel-body">
{{#each this}}
<h1 class="fs30 mt5 mbn">{{this}}</h1>
{{/each}}
<h6 class="text-success">Most {{#key}}</h6>
</div>
{{/each}}

#each inside another #each that is not a part of the first one handlebars

I got a problem with displaying values inside "main" loop
my code looks like this:
{{# each movies}}
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<!--<img src="..." alt="...">-->
<div class="caption">
<h3>{{this.title}}</h3>
<p>Rating :
{{#each ../ratings}}
{{this}}
{{/each}}
</p>
<p>Genre: <b>{{this.genre}}</b></p>
<p>{{this.description}}</p>
<p>Modify Delete</p>
</div>
</div>
</div>
{{/each}}
And the problem is that I get all three values of "ratings" list inside the same block, but I want to get them separately one per each of movies. Any suggestions? Thank you in advance.
I needed to change my model and include rating field to it. So I didn't need the second each loop at all.

Handlebarsjs change each helper context

I have a nested each helper inside an each helper. i want to accsess a variable from the outer each helper inside the inner each helper. code bellow
{{#each this=outer}}
<li>
{{each this.values}}
<input type='radio' value='{{this}}' data-id='{{outer.id}}'>
{{/each}}
</li>
{{/each}}
the above syntax does not work
You can use a parent context using ../ inside the variable.
I'm not sure how your data is structured but your template should probably look something like this:
{{#each outer}}
<li>
{{each outer.values}}
<input type='radio' value='{{this}}' data-id='{{../outer.id}}'>
{{/each}}
</li>
{{/each}}
Check out this github issue for more.

How to loop through array inside an array in handlebar with Node.js + express

I have JSON array as follows:
{outer:[
{
Key1:"ID001",
Key2:[
{
innerKey1:"Myval1",
innerKey2:"Myvalue2"
}
]
}
]}
Now in my HTML file :
<div>
{{#each outer}}
<b>key1:</b> {{this.Key1}} <br/>
{{#each this.Key2}}
<b>InnerKey1:</b> {{this.innerKey1}} <br/>
{{/each}}
{{/each}}
</div>
But it not showing up any inner values. Could anyone please help how to loop on inner array objects (i.e Key2 above). Do I need to write a separate helper for this?
You must choose between object or array interation:
{
outer:[{
Key1:"ID001",
Key2:{
innerKey1:"Myvalue1",
innerKey2:"Myvalue2"
},
Key3:[
"Myvalue4",
"Myvalue5"
]
}]
}
<div>
{{#each outer}}
key1: {{this.Key1}} <br/>
{{#each this.Key2}}
{{#key}}: {{this}}
{{/each}} <br/>
{{#each this.Key3}}
{{#index}}: {{this}}
{{/each}}
{{/each}}
</div>
outputs:
key1: ID001
innerKey1: Myvalue1 innerKey2: Myvalue2
0: Myvalue4 1: Myvalue5
http://codepen.io/rafaelcastrocouto/pen/qgrFE

Resources