简体   繁体   English

如何使用把手中的键访问上下文对象中的值?

[英]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: 我的highlight部分看起来像:

<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: 这是我的javascript对象的样子:

在此处输入图片说明


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). 我正在使用3.0.3(最新)把手。

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];
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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