1. Handlebars Helpers
  2. Dynamic Lookup

Handlebars Helpers

Dynamic Lookup

Allows returning a value from an object based on a key

Uses

This helper allows you to return a value from an object based on a key.
This is helpful when you have a data structure that contains many elements that are identified by some key.
You can read the lookup key from the data model and return the corresponding value from the object dynamically.

Usage

Helper name : dynamic_lookup

Parameter Description
1 data structure to lookup the key in
2..n key paths to lookup in the data structure (each key path will be concatenated to do the lookup)

The key path can be a string or a variable.
The key path could be a string with dot notation to access nested objects.

Examples

Let's imagine you had the following data in the stub and in the stubpost:

loading...

Simple Example

Then we could dynamically select the color setting based on the color selected in the stubpost data.

loading...

This would return the value 00FF00 as the color selected in the stubpost data is green.
The data saved would be :

loading...

Dynamic Example

Let's say we would like to get John's age based on the person_name in the stubpost data.
Then we would pass two parameters to the helper, the data structure to lookup the key in and the key to lookup in the data structure.

loading...

This would return the value 25 as the person_name in the stubpost data is john.

We could also get John's first pet type by passing the key path as a string separated with dots (0 denoting the first element in the array).

loading...

This would return the value dog as the person_name in the stubpost data is john.