Customise a search result for multiple content types
Managed by | Updated .
Prerequisites
- Collection has been crawled with the metadata contentType mapped for each result.
- Completed How-to-Guide: Customise a search result.
- Understanding of the Freemarker Templating Language.
- Understanding of the Funnelback data model.
- Download this file result.ftl.
Objective
Customise a search result to have a different display for each content type. In this example we will customise the result for a page Page, Profile and FAQ view.
Steps
1. Upload result.ftl
- Load the Funnelback administration interface and select the the collection (search interface) you would like to customise.
- Open the file manager.
- Select Administer tab > Browse Collection Configuration Files
- Upload result.ftl file.
- Under Presenation (preview) section select Choose File and select the result.ftl file saved on your computer.
2. Import Result Template into Simple.ftl
- Under Presenation (preview) section select simple.ftl for editing.
This is the main search template file which handles the display of the entire search interface. - At the top of the file add this line after the existing imports.
This imports our new template libaray into the current file.<#import "result.ftl" as Result/>
- Locate the results block (the code between <@s.Results> and </@s.Results>).
Tip: To quick find the result block click within the file edit and press Ctrl(control)+f or mac ⌘(command)+f and search for s.Result. - Delete all the code within
and replace with the following code.<li data-fb-result=${s.result.indexUrl}>
This code calls the ResultRoute marco provided within result.ftl.<@Result.ResultRoute />
- Click Save and publish.
- Open the search page result and see how the result view has now changed and displays the text "Customise your result format for each content type!" repeated for each result.
The provided result.ftl includes a few pre implemented macros;
- IsContentType - This is used to test the condition for the result content type.
Note: i have this mapped to the metadata field contentType but this could be set to any metadata field required to test against. - Result Route - This is an empty marco which you will configure in the next set "Define your result formats". This marco will be used to define the conditional logic for which result format to display.
- Page,Profile, FAQ - These are the result formats that will be used when the matching condition is met within the ResultRoute macro.
3. Define your result formats
- Under Presenation (preview) section select result.ftl for editing.
- Replace the code within the ResultRoute marco with the following.
This code will route the search result for to use the correct display for the corresponding content type.<#-- Page -->
<@IsContentType name="Page">
<@Page />
</@IsContentType>
<#-- Profile -->
<@IsContentType name="Profile">
<@Profile />
</@IsContentType>
<#-- FAQ -->
<@IsContentType name="FAQ">
<@FAQ />
</@IsContentType> - Click Save and publish
4. View & Test
- Go to the result page an view the new result format for results.
Working Example
Was this artcle helpful?