简体   繁体   中英

How to access value in context object with key in Handlebars?

I have a handlebars template that looks like:

<script id="user-template" type="text/x-handlebars-template">
  <a class="result" href="/{{github_id}}">
    <img src="{{image_url}}" />
    <span class='additional-name'>{{> highlight object=this key="github_id"}}</span>
  </a>
</script>

And my highlight partial looks like:

<script id="highlight-search-partial" type="text/x-handlebars-template">
  {{#if object._highlightResult}}
    {{#if object._highlightResult.key}}
      {{object._highlightResult.key.value}}
    {{/if}}
  {{else}}
    {{object.key}}
  {{/if}}
</script>

Here's what my javascript object looks like:

在此处输入图片说明


After rendering the handlebars template, visually it's empty. Nothing is being rendered.

Any ideas on what I'm doing wrong? I'm using handlebars 3.0.3 (latest).

I ended up writing a helper handlebars function:

<span>{{highlight this "github_id"}}</span>

Handlebars.registerHelper('highlight', function(obj, field) {
    if (obj['_highlightResult']) {
      return obj['_highlightResult'][field].value;
    } else {
      return obj[field];
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM