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}}
Related
I'm trying to pass a variable (tag name) into a Handlebars partial and use an #is block helper on the tag but for some reason it just won't play ball. This is my code:
Call to my partial and passing through the tag name.
{{> nav tagged='page' }}
In the partial itself I do the following (tagged is the variable name passed through):
{{#each tags}}
{{#is tag tagged}}
{{#each pages}}
// Do code here
{{/each}}
{{/is}}
{{/each}}
If I just render the tagged variable it displays the variable value as expected so a bit confused as to why its not working.
Thanks.
The issue you have is that the tagged variable is in the parent context but you're trying to reference it within the #each tags loop.
You can reference the parent context with ../ so the working code would be
{{#each tags}}
{{#is tag ../tagged}}
{{#each pages}}
// Do code here
{{/each}}
{{/is}}
{{/each}}
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
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}}
I have the following template:
{{#each Posts}}
{{#with { Post: this } }}
<h2 on-click="doSomething">{{Title}}</h2>
...
{{/with}}
{{/each}}
When I click on the header and doSomething get called, I get "${{Post:Posts-0}}" in event keypath. But I need to get access to the post keypath: "Posts.0" to modify some of its properties. What is the right way to achieve that?
Using a {{#with { a: b } }} block for aliasing in Ractive has some limitations, as it's not true aliasing, it's simply creating an expression with an object literal. It's a needed enhancement to offer true aliasing with something like {{#each Post in Posts}} or {{#each Posts as Post}}.
As far as what you can do today, you can add the keypath to the with block:
{{#with { Post: this, keypath: #keypath } }}
And then either pass in:
<h2 on-click="doSomething:{{keypath}}">{{Title}}</h2>
Or access in the event via this.event.context.keypath. See http://jsfiddle.net/w0npbnrz/ for both of these in action.
You also could use {{#each Posts:p}} in which case you could get the keypath via 'Posts.' + this.event.index.p.
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.