I've been trying to use blaze Each with a Each inside
ColA being one collection
ColB another different collection (not inside ColA)
{{#each colA}}
{{#each colB}}
{{this}} // refer the outer Loop?
{{/each}}
{{/each}}
like this guy asked on this Stack Post but when i try that the app brokes. Seems its pretty old... 2012.
Is there an effective way to do that ?
Thanks!
Assuming
ColA has: title, author
ColB has comment, user
{{#each colA}}
{{#each colB}}
{{ ../title }} by {{ ../author }}
{{ comment }} by {{ user }} // refer the outer Loop?
{{/each}}
{{/each}}
Related
Currently I can get custom fields all or none. Like this
{{#each product.custom_fields}}
{{ id }} : {{ name }} : {{ value }}
{{/each}}
but what if i want to call just one of them by id or name like the below. Is there a way to do that with stencil or otherwise?
{{product.custom_fields.id:4.name}}
You can select a list item by id. {{product.custom_fields.id.4.name}}, however, if you want to select by name you'll need to implement a conditional as #alyss suggested.
See: How do I access an access array item by index in handlebars?
You can use the existing {{if}} helper to accomplish this
{{#if display_name '===' 'material'}}
{{#each product.custom_fields}}
{{id}} : {{name}} : {{value}}
{{/each}
{{/if}}
I have nested each and want to use parent this value.
{{#each county}}
Country name : {{this}}
{{#each state}}
{{this}} is one of the state of {{country}} //here how to use country
{{/each}}
{{/each}}
I have tried {{../this}} but it shows
Can only use `this` at the beginning of a path.
Instead of `foo.this` or `../this`, just write `foo` or `..`.
simply you can do
{{#each county}}
Country name : {{this}}
{{#each state}}
{{this}} is one of the state of {{..}} //here how to use country
{{/each}}
{{/each}}
see {{..}}
EDIT: edited answer
So I'm building my first app with meteor, and I feel like I'm repeating myself with my templates more than I should be.
I have multiple parent views, an example of which is the user contacts view, and the add group members view. (simplified examples below.)
<template name="GroupMembers">
{{#each contacts}}
{{> contact }}
{{/each}}
</template>
<template name="contacts">
{{#each contacts}}
{{> contact }}
{{/each}}
</template>
<template name="contact">
//... single contact template stuff
</template>
When the contact is displayed in the contacts list, I want to display a remove from contacts link in the single contact template, but in the group members list I'd like an 'add to group' link in its place. I know I could probably achieve this with either session variables or by invoking the iron-router controller obj, but I'd like to know if there is a simple way to do this in the template helper(s). Or put another way can these template partials become context aware?
Any help would be great.
Thanks.
I would solve it this way:
<template name="GroupMembers">
{{#each contacts}}
{{> contact groupMembers=true}}
{{/each}}
</template>
<template name="contacts">
{{#each contacts}}
{{> contact }}
{{/each}}
</template>
<template name="contact">
<p>
{{#if groupMembers}}
{{../name}}
<button>add to group</button>
{{else}}
{{name}}
<button>delete</button>
{{/if}}
</p>
</template>
Live demo: http://meteorpad.com/pad/LDTvHC787kJ6e9JQA/Leaderboard
What is the standard way to access outer #each collection values in the loop?
for example:
<template name="example">
{{#each outerCollection}}
<tr>
{{#each innerCollection}}
<td>{{aaa}}</td>
{{/each}}
</tr>
{{/each}}
</template>
Template.example.aaa = function(){
// cannot access outerCollection values
}
in above Template.example.aaa, this points to the inner collection.
I cannot find way to access outerCollection items.
My solution is like below, I am defining my own helper function.
Is it a standard Meteor way to achieve this purpose?
<template name="example">
{{#each outerCollection}}
<tr>
{{#each innerCollection}}
<td>{{myHelper ../outerItem innerItem}}</td>
{{/each}}
</tr>
{{/each}}
</template>
Handlebars.registerHelper('myHelper', function (outItem, inItem) {
// can access outerCollection via outerItem
});
I found a similar question for the case of inner event handler access.
I think you've answered this yourself! Using ../ is documented in https://github.com/meteor/meteor/wiki/Handlebars.
You can use below code to fetch outer collections.
suppose you have collection called as Collection.Customer and Collection.RechargePlan and you are using both in a template for updating Customer.
Customer = {"name":"James", "rechargeplan":"monthly"};
RechargePlan = [{"rechargeplan": "monthly"},{"rechargeplan": "yearly"}];
//Inside template, Bydefault Customer is available.
{{#each RechargePlan}}
{{#if equals ../rechargeplan rechargeplan}}
//Hurray Plan matches
{{/if}}
{{/each}}
In above code, ../rechargeplan is actually Customer.rechargeplan, ../ actually went one step above heirarchy and then accessed the field if available, since Customer is already available to template, it's field is picked up.
Is there a better way?
{{#if firstName }} {{ firstName}} {{/if}}
{{#if middleName }} {{ middleName}} {{/if}}
{{#if lastName }} {{ lastName}} {{/if}}
I often have missing data.
You can also use
{{#with firstName}} {{this}} {{/with}}
Best way would be to save the name as an object {name:{first:'James', last:'Bond'}} and then:
{{#with name}}{{last}}, {{first}} {{last}}{{/with}}
With handlebars if the value is null or empty handlebars will spit out an empty string as Nick Kitto mentioned.
so just {{firstName}} {{middleName}} {{lastName}} would be fine. However, if the middle name were blank you'd end up with 2 spaces between the first and last name, that's about the only possible problem I can see.