Gravatar for

Question by Arulselvan, Feb 20, 2018 7:35 PM

combine two fields in Result view

I want to combine two fields value in to the title link in the search result view.

I tried to do like below code,

<h4 class="coveo-title">
    <a class='CoveoResultLink' target='_blank'>{{= raw.@(Model.ToCoveoFieldName("title", false)) ? highlightStreamText(raw.@(Model.ToCoveoFieldName("title", false))","raw.@(Model.ToCoveoFieldName("subtitle", false))) : clickUri }} </a>

The expectation is the result should be: "Hello, World"

Here "Hello" is title and "World" is subtitle fields separated by ",".

Please suggest on this.

2 Replies
Gravatar for

Answer by Bala, Feb 20, 2018 7:45 PM

I have used Coveo result templates for this kind of situations with Underscore templates and custom JavaScript methods that will help you achieve this.

Coveo Result Templates

<script class='result-template' type='text/underscore'>
   <span class="label">presenter</span>:
<script type="text/javascript">
   function getPresenterName(firstName, lastName) {
               return firstName + lastName;
Gravatar for

Comment by Arulselvan, Feb 20, 2018 8:41 PM

Thank you!! Appreciated.

Gravatar for

Answer by Jean-François L'Heureux, Feb 21, 2018 12:40 AM

Everything between `{{ }}` is pure JavaScript.

Concatenating strings in JavaScript is not achieved by adding a comma between them but by adding the plus sign between them.

So in your example, it would be:

<h4 class="coveo-title">
  <a class='CoveoResultLink' target='_blank'>
    {{= raw.@(Model.ToCoveoFieldName("title", false)) ? highlightStreamText(raw.@(Model.ToCoveoFieldName("title", false))) + (raw.@(Model.ToCoveoFieldName("subtitle", false)) ? highlightStreamText(raw.@(Model.ToCoveoFieldName("subtitle", false))) : "") : clickUri }}

I also modified the code to be more robust in case there is no subtitle on a search result.

Ask a question