2017-06-14 21 views
0

我目前正在使用Algolia + Hogan模板。但我經常遇到以下錯誤,我在CraftCMS上運行Algolia。Hogan使用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)

回答

1

沒有必要自己編譯模板,你可以做:

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

,並應解決您的問題。

+0

感謝您的回答 - 不幸的是,現在我收到以下信息: **未捕獲的TypeError:無法讀取屬性'textContent'null ** – user3082823

+0

通過移動腳本在模板下方移除錯誤 - 但沒有命中模板呈現。 – user3082823

+0

也沒有控制檯錯誤返回:( – user3082823

1

通過刪除{{#hits}}塊並添加@vvo答案進行修復。