Skip to content Skip to sidebar Skip to footer

Accessing Array/object Variables By Key, Within A Handlebars Mustache

I can't seem to find a way of doing this, and have tried subexpressions and various other answers posted on StackOverflow - but they all seem to assume that I know the key I'm usin

Solution 1:

I would not create a custom helper for this. Instead, I would use the existing Lookup helper partnered with the with block helper:

{{#with (lookup fields field_id) as |field|}}
    {{field.complete}}
{{/with}}

Alternatively you could use the lookup helper with a subexpression:

{{lookup (lookup fields field_id) 'complete'}}

Solution 2:

I made this work by registering a custom helper:

Handlebars.registerHelper( 'getfield', function ( data, field_id, key ) {
    var val = ( data[ field_id ] ) ? data[ field_id ][ key ] : '';
    return val;
} );

and then made use of this in my templates with:

{{getfield fields field_id 'complete'}}

Post a Comment for "Accessing Array/object Variables By Key, Within A Handlebars Mustache"