Templates let you customize the display of your autocomplete items.
Autocomplete is also available as an experimental widget in InstantSearch, making it easier to integrate into your search experience.
To learn more, see the API reference for InstantSearch.js or React InstantSearch.
Once you’ve set up your data sources,
you need to define how they display.
Autocomplete templates let you customize the appearance and layout of each item.
Autocomplete uses a virtual DOM for rendering.
This ensures optimal performance even with frequent updates,
safeguards against cross-site scripting (XSS) attacks, and supports inline event handling.Templates can return any valid virtual DOM elements (VNodes).For example, templates can return strings, HTML strings, or Preact components.
Native HTML elements aren’t valid VNodes,
which means you can’t return a template string that contains HTML,
or an HTML element.
But if you’re not using a virtual DOM implementation in your app,
you can still return HTML with the provided html tagged template.Every Autocomplete template provides an html function that you can use as a tagged template.
Using html lets you provide templates as an HTML string
It works directly in the browser without a build step.
The html function is only available starting from Autocomplete v1.6.0.
Passing a unique key attribute is helpful when mapping over items. It helps the virtual DOM keep track of each element when they change, and update the UI efficiently.
To conditionally render a part of your UI, use a short-circuit or a ternary operator.
Only the HTML provided as a string literal is evaluated. HTML in variables (for example, stored in Algolia records) isn’t supported and is rendered as-is.
JavaScript
Report incorrect code
Copy
import { autocomplete } from "@algolia/autocomplete-js";autocomplete({ // ... getSources() { return [ { // ... templates: { item({ item, html }) { const name = "<p>Jimmie Barninger</p>"; // This returns a `<div>` with text "<p>Jimmie Barninger</p>" return html`<div>${name}</div>`; }, }, }, ]; },});
You can’t use html as a tagged template in Internet Explorer 11.
Depending on your project setup, you can work around this problem to still use html while providing compatible code to your Internet Explorer 11 users.
If you have a build step and are using Babel to compile your code for legacy browsers, you can transform all html expressions into regular function calls.The recommended setup is to use @babel/preset-env, which provides this transformation along with other common ones based on a list of browsers to support.
If you don’t have a build step in your project,
write a .
Tagged templates are regular functions with a specific signature, so you can wrap their calls with a friendlier API to avoid using tagged template notation.This function takes templates as static strings or an array of interspersed chunks, splits them, and passes them to the html function.
Report incorrect code
Copy
function htmlShim(template, html) { if (typeof template === "string") { return html([template]); } const parts = template.reduce( (acc, part, index) => { const isEven = index % 2 === 0; acc[Math.abs(Number(!isEven))].push(part); return acc; }, [[], []], ); return html(parts[0], ...parts[1]);}
The documented shim assumes every even array entry is a template string, and every odd entry is a dynamic value.
Change the shim if you need it to do something different.
The provided html function works in the browser without any build step, with a negligible impact on memory and bundle size (< 600 bytes).
To ensure optimal performance,
use babel-plugin-htm to compile HTML tagged templates.
This plugin replaces html calls, so your code must expose a consistent parameter name to the compiler.
Avoid destructuring directly in the function signature.
Instead, either name the parameter and destructure it inside the function body,
or pass the object as-is.
The parameter name (for example, params) must be consistent across all templates.
By default, createElement and Fragment default to Preact’s preact.createElement (or h) and preact.Fragment.
If you’re using another virtual DOM implementation, you can replace them.
Templates expose a set of built-in components to handle highlighting and snippeting.
Use these components to highlight or snippet results from the getAlgoliaResults function.
A set of templates to customize how items are displayed.
You can also provide templates for header and footer elements around the list of items.You must define templates within your sources.