Leverage a hogan template within the instantsearch.js widget

I'm working on integrating Algolia's InstantSearch.js into my project. Since the search results will contain a lot of HTML, I want to organize it in a Hogan template. The search results seem to be loading, but nothing is being displayed on the screen?

<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>

Any assistance with this issue would be greatly appreciated.

Answer №1

If you don't feel comfortable using Hogan, just provide us with the template:

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

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

Don't forget to monitor the console for any errors. Appreciate your help!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Prevent certain individuals from selecting a checkbox within HTML forms

Currently, I have a form where users need to fill in their information. However, I want certain parts of the form to be preventable based on the level of access the user has. For example, I have successfully implemented a code snippet onfocus='blur() ...

how to name a collection variable in MongoDB shell using JavaScript

When using the mongo shell with JavaScript, is it possible to dynamically set the collection name in order to work with multiple collections? db.collection.insert() ...

Logging confidential information in Javascript

As I configure logging to include user passwords in a JSON object being sent to the backend, I am faced with the need to exclude the password value from the logs. The structure of the object is as follows: e.g, { "username": my_username, "passwor ...

Utilize useNavigate() in React to redirect users following form validation and submission, implementing routing alongside a loader and defined action

Recently, I created a demo site that features a form for submitting new user information. The following are the key files: App.jsx: import React from 'react'; import './App.css'; import { RouterProvider, createBrowserRouter } from ...

Finding the current div based on the scrolling position can be achieved without the use of jQuery

Seeking assistance with a coding challenge. I have a solution in jQuery, but we require the solution in plain JavaScript or Angular. It seems like something is missing. Take a look at this fiddle. The objective is to highlight the corresponding left panel ...

Using AngularJS to bind elements within elements

Feel like I might be overlooking a simple solution here, but I'm facing an issue: <h4 ng-bind="example.heading"> <small ng-bind="example.subheading"></small> </h4> This code doesn't seem to work - if ng-bind replaces ...

I attempted to send an email but received an error stating that Access to XMLHttpRequest from origin 'null' has been restricted by the CORS policy

As a beginner in PHP and JS, I recently created a form to send contact information via email. The JS file used for this process is shown below: $(function () { $("#contactForm input, #contactForm textarea").jqBootstrapValidation({ pre ...

What steps can I take to avoid random divs from overlapping on smaller screens when dealing with elements created in the created() hook?

I encountered an issue with the code I'm working on (the circles overlap in the fiddle provided, but display correctly on my PC - possibly due to pixel discrepancies in the fiddle). You can find the code here. TestCircle.vue: <template> <d ...

Issue with large date changes causing MUI DatePicker to freeze

The MUI DatePicker, whether from Labs or X, was functioning perfectly. However, I am now facing an issue where it hangs when trying to change the date by ten years. It appears that the code gets stuck in an endless loop, yet there are no errors displayed i ...

Establishing the Time-to-Live (TTL) with Redis-OM and Node Object Mapping

Recently, I stumbled upon the exciting new Redis-OM Node Object Mapping feature. Although I have limited experience with Redis, I am eager to dive into it now. Currently, I have a basic function in place for creating rooms but I want these rooms to automa ...

Error in Next.js: react-dom.development.js?ac89:14906 - Hook call is invalid. Hooks should only be used within a function component's body

Here is the code snippet I am working with: <div onClick={(e) => handleClick()}>Join Us</div> This is the handleClick function in my code: const handleClick = () => { console.log(Lang.getLocale()) }; And this is the Lang class metho ...

Should you construct a fresh element using JavaScript, or opt to reveal a concealed one using CSS?

What are the benefits of using the following approach: var target = document.getElementById( "target" ); var newElement = document.createElement( "div" ); $( target ).after( newElement ); compared to just hiding the newElement div with CSS and showing ...

How to obtain the current URL of an iframe?

After coming across what seems to be a similar question, I still have to ask for clarification because I couldn't find a clear answer. Allow me to elaborate on my project, I am working on an online store where we offer two types of products, The fi ...

Using Laravel to set cookies with Ajax

I am facing difficulties in setting cookies through laravel using ajax. Despite reading several questions and posts, I have not been able to find a solution. My issue involves a dropdown that triggers a javascript function to send its value to a controlle ...

How does setting $.support.cors = true; affect the performance of ajax calls on browsers that do not support Cross-Origin Resource Sharing (

Hey everyone, I've encountered a situation where I need to make cross-domain AJAX requests, so I included the line "$.support.cors = true;" before my ajax calls. However, I'm noticing that for non-cross domain calls, my ajax requests don't ...

Produce a vue attribute

I am looking to display a property in my component. My template includes: <v-flex v-for="c in components"> <component :is="c.component" v-bind="c.prop"></component> </v-flex> And in the script: ... mounted(){ ...

Looking to output JSON-parseable JSON using PHP's json_encode function? Avoid the "Unexpected token h" error caused by single-escaped double quotes by following

Below is a snippet of my PHP code: <?php $jsonarr = array("haha" => array("hihi'hih","hu\"huh")); print json_encode($jsonarr); When executed, the output looks like this: {"haha":["hihi'hih","hu\"huh"]} However, when I ...

The first parameter in Vue router is optional

Is there a way to make the first parameter in my list of routes optional? I want the parameter to change a header in my api calls if it's present in the url, but everything else should stay the same. Instead of creating two sets of routes to handle t ...

Darkness prevails even in the presence of light

I'm currently in the process of developing a game engine using ThreeJS, and I have encountered an issue with lighting that I need assistance with. My project involves creating a grid-based RPG where each cell consists of a 10 x 10 floor and possibly ...

Encountering an issue with accessing the 'username' property when using JWT authentication in an Express application

When implementing a JWT-based login/signup feature on Express.js with MongoDB, I encountered an error while trying to match the user input with the database username: TypeError: Cannot read property 'username' of undefined This is the code sn ...