javascript - 使用 Algolia 进行 Hogan 模板化

标签 javascript templates algolia hogan.js instantsearch.js

我目前正在使用 Algolia + Hogan 进行模板化。但我经常遇到以下错误,我在 CraftCMS 上运行 Algolia。

<script>

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

    const search = instantsearch({
      appId: '{{ craft.searchPlus.getAlgoliaApplicationId }}',
      apiKey: '{{ craft.searchPlus.getAlgoliaSearchApiKey }}',
      indexName: 'products',
      urlSync: true
    });

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

    search.start();

</script>

这是我的热门模板。原始的,因为 CraftCMS 会从 .

{% raw %}

  <script type="text/template" id="hit-template">
          {{#hits}}
          <li>
              <h4><a href="{{ absoluteUri }}">{{ title }}</a></h4>
              {{{ description }}}
              <p>{{#productImage}}</p>
                <img src="{{ url }}"/>
              {{/productImage}}
          </li>
          {{/hits}}
  </script>

{% endraw %}

通过控制台收到以下错误。

Warning: Failed prop type: Invalid prop templates.item supplied to t.

Template.js:117 Uncaught Error: Template must be 'string' or 'function', was 'object' (key: item)

最佳答案

不需要自己编译模板,你可以这样做:

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

这应该可以解决您的问题。

关于javascript - 使用 Algolia 进行 Hogan 模板化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44552171/

相关文章:

javascript - meteor :如何使用模板事件检索 "{{this}}-value"

html - 是否可以使用 Rollup 创建一个从模板注入(inject) HTML 的包?

ios - 在 tableView cellForRow containsHit 方法之前访问 Algolia InstantSearch 命中

java - 如何检索所有方面?

c++ - 循环替换模板

javascript - 使用 algoliasearch 将 href 更改为半硬编码变量

javascript - 如何在 DOJO 中禁用向导 Pane 的 "Next"按钮

php - 使用 jquery 替换 youtube 嵌入代码中的字符串

javascript - 如何正确构建包含许多子级别的列表

javascript - 指令/范围继承