Handlebarsjs change each helper context - node.js

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.

Related

handlebars each in each arguments

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>

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}}

Handlebars JS passing parent array into partial as parameter

I'm trying to pass an array from the parent context into a partial as a parameter. I've read numerous SO articles and tried all the suggestions and so far I can't get it to work. The best I've gotten so far is the the array shows in the partial as [object Object]. That's the closest I've gotten :-(
In my parent layout I have...
{{> dropdown dd-id='appId' dd-text='App' dd-options=this.app }}
app is set to an array of objects like: [{ "id": "blah...", "text": "blether..." }]. It has only 1 line in the array.
In the partial is...
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button"
id="{{ dd-id }}" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ dd-text }}
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
{{#each dd-options }}
<a class="dropdown-item" id="{{ id }}">{{ text }}</a>
{{/each}}
</div>
</div>
In the parent rendered page, the dropdown looks like a Bootstrap button dropdown, so that's good, but the options shows...
[object Object] instead of the single option row with id and text that I need it to show.
What am I doing wrong? Thank you for sharing your expertise! :-)
I ran into this problem as well and it seems handlebars.js doesn't support sending arrays (or objects) to partials using named parameters. See this issue:
https://github.com/wycats/handlebars.js/issues/1058

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.

Resources