在 instantsearch.js 小部件中使用霍根模板

Use a hogan template in instantsearch.js widget

我正在尝试实施 algolias instansearch.js。我的搜索结果将有很多 HTML,所以我想将其提取到 hogan 模板中。结果似乎正在加载,但没有呈现任何内容?

<script type="text/template" id="hit-template">
  {{#hits}}
  <div class="hit">
    <div class="hit-image">
      <p>test: {{ objectID }}</p>
    </div>
  </div>
  {{/hits}}
</script>

<script>
var hitTemplate = Hogan.compile($('#hit-template').text());

search.addWidget(
  instantsearch.widgets.hits({
container: '#hits-container',
templates: {
  empty: 'No results',
  item: function(data){
    return hitTemplate.render(data);
      }
    },
    hitsPerPage: 6
  })
);
</script>

如有任何帮助,我们将不胜感激

您不需要自己使用Hogan,只需给我们模板:

var hitTemplate = document.querySelector('#hit-template').innerHTML;

search.addWidget(
  instantsearch.widgets.hits({
    container: '#hits-container',
    templates: {
      empty: 'No results',
      item: hitTemplate
    },
    hitsPerPage: 6
  )
);

同时检查控制台是否有错误消息。谢谢