Skip to content

template render a list of items with HTML #998

Answered by sarahdayan
akeni asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @akeni,

This documentation section explains how to deal with loops when using html.

import { autocomplete } from '@algolia/autocomplete-js';

autocomplete({
  // ...
  getSources() {
    return [
      {
        // ...
        templates: {
          item({ item, html }) {
            return html`<div>
              <img src="${item.image}" alt="${item.name}" />
              <div>${item.name}</div>
              <ul>
                ${item.categories.map(
                  (category) =>
                    html`<li key="${category.id}">${category.label}</li>`
                )}
              </ul>
            </div>`;
          },
        },
      },
    ];
  },
});

If you can't make …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@akeni
Comment options

Answer selected by akeni
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants