Configuring a New Result Template

This topic covers how to configure a result template in Coveo for Sitecore Hive.

To learn how to create a new result template, see Coveo JavaScript Search Framework - Result Templates.

Item Result Template

This result template is stored directly in the item.

You need to configure the Script field with the result template you want to use.

Sample

Copy-paste this sample in the Script field of your result template and save the item.

Example Result Template

<div style="padding: 5px;">
  <div class="my-result-template-header" style="display: flex; align-items: center;">
    <div class="CoveoIcon" data-small="true"></div>
    <div class="CoveoResultLink" data-field="{{= coveoFieldName("@title") }}" style="flex-grow: 1; margin-left: 5px;"></div>
  </div>
  <div class="my-result-template-content">
    <div class="CoveoExcerpt"></div>
  </div>
</div>

File Result Template

A file result template is saved on-disk, in the YOUR_WEBSITE/Coveo/Hive/templates folder.

You need to configure the Template Name field with the name of the file to use.

Sample

Coveo for Sitecore Hive already provides a default template named default.cshtml.

To use it, in File Template Name, enter default. This loads the default template.

If you need to modify this template, make a copy of it instead, and modify that copy.

Conditions

Conditions allows you to configure specific views for specific item types and show different information for each of them.

For instance, if you want to render a specific view only when the result item is based on a given Sitecore template, you could add the following:

The result template component will now add a data attribute that determines the conditions for this template, like the following:

data-field-z95xtemplate="884ED7509E18464DA437D558FEFC5370".

Coveo for Sitecore Hive automatically translates the conditions field name to the Coveo format.

When the results are received from Coveo, if one result has the z95xtemplate field set to 884ED7509E18464DA437D558FEFC5370, it will be rendered using that template. If z95xtemplate is not set on the result, or set to another value, the next result template from the list will be evaluated instead.

For more information on what template is chosen when trying to render a result, see What Determines Which Template Loads for a Result

The order in which you declare your templates is important, as the first template with matching conditions is used.

If you leave a template without conditions at the top of the list, the other templates in the list will never be evaluated since the first one will always have matching conditions.

It is instead recommended that you leave your default template without conditions at the bottom of the list as a proper fallback.

Example

In this example, there are three results templates: author, book, and default.

  1. In the Result Template section, select Add here.
  2. Choose File Result Template and click Select.
  3. Create a new Data Source and name it something easily recognizable. For this example, you can use Author Result Template.
  4. Click on the newly added rendering, and click on the Edit Data Source button.
  5. Edit the Template Name to the name of your template and add some conditions matching your specific item. For this example, you can follow these steps:
    1. On the author template, set a condition that matches only the items that are authors: _template==BCB117C25E624A8ABC2199F03E629354.
    2. On the book template, set a condition that matches only the items that are authors: _template==884ED7509E18464DA437D558FEFC5370.
    3. The default template has no condition and is used as a general fallback for other items.

Here is what the final result can look like:

The Book item matches the book template and is now rendered differently than authors and other items.

Referencing the Template in Multiple Results List

If you are leveraging the Coveo Result Layout component or using multiple result list, you can easily reference already created result template data sources.

These steps allows you to add the same templates from the previous example in a Card layout.

  1. Select the Card Layout button, located in the Result Layout section.
  2. Select the displayed result list Result Template placeholder, and add a new component
  3. Choose File Result Template and click Next.
  4. Select the previously created result template.
  5. Save the page.

The same template should now be shown in the card layout.

Here is the final result: