Example of using Handlebars lookup helper

后端 未结 2 1441
耶瑟儿~
耶瑟儿~ 2021-01-07 17:07

Handlebars has a built-in helper called lookup. The documentation is not very clear about how it works. Could I see an example?

2条回答
  •  天涯浪人
    2021-01-07 17:23

    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:

    {{book.title}}

    Which is equivalent to:

    {{lookup book 'title'}}

    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:

    {{lookup book property}}

提交回复
热议问题