Example of using Handlebars lookup helper

The lookup property is useful if we don't know the name of the property we want, for instance because it's in a variable or the result of an expression.

If we have this object:

var book = {
    title: 'Discovery of Heaven'
};

We could put this in the HTML like this:

<p>{{book.title}}</p>

Which is equivalent to:

<p>{{lookup book 'title'}}</p>

Maybe we don't know that we want the title. Say the property name is somewhere in a variable instead:

var property = 'title';

Now we could show the book title like this:

<p>{{lookup book property}}</p>

Sure, past me! Here's an example from your future.

Suppose you have an object or array obj and a variable field and you want to output the value of obj[field], you would use the lookup helper {{lookup obj field}}.

The code defining the helper is simply:

function(obj, field) {
  return obj && obj[field];
}