Display Template Series
In my previous post Brief Introduction into Display Templates I gave an overview of what a Display Template is. In this post I hope to delve a little deeper and show how they are used with Content Search WebParts.
When you add a Content Search WebPart or a Search Result WebPart when you configure the Web Part, you have to select a control and an item display template.
A Control Template is used to show how the results are presented, an HTML structure of the overall layout. The surrounding area that will contain all items returned. For example it could be the heading and footer. The control template is only rendered once within the WebPart.
An Item Template provides the HTML display for an item in the search results. This template will be called for each item in the result set. This allows you to define the layout of the item.
So when the WebPart uses them together it creates a rendered piece of HTML for the webpart.
A display template structure is made up of four main sections. Title, Header properties, Script Block and a DIV block.
Very simply this is the display name that is found in the Display Template sections of the WebPart edit pane.
<title> My Display Name </title>
Following on from the title tag, there is a set of custom elements which provide information about the display template to SharePoint. These elements are surrounded by the following XML markup:
<!--[if gte mso 9]><xml>
An example of an element would be;
<mso:MasterPageDescription msdt:dt="string">Displays a result tailored for a picture.</mso:MasterPageDescription>
The different properties and descriptions are described here (Taken directly from the Microsoft Website):
||Boolean value that indicates whether to hide the display template from the list of available templates in the Web Part. This value can be changed in the display template file properties.
||Provides a friendly description of the display template. This is shown to users in the SharePoint editing environment. This value can be changed in the display template file properties.
||The ID of the content type associated with the display template
||Indicates the context in which the display template is used. This value can be changed in the display template file properties.
||Boolean value that indicates whether a display template HTML file has a .js file associated with it.
||Indicates whether the conversion process was successful. This value is automatically added to the file by SharePoint, and is used only in custom display templates.
||Contains the URL to the HTML file and the text for the Status column (either Conversion successful or Warnings and Errors). This value is automatically added to the file by SharePoint, and is used only in custom display templates.
$includeScript(this.url, <a href="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js</a>);
$includeCSS(this.url, "style library/Slider/css/MySearchWebPartStyle.css");
Example below taken from the Control_List.html file.
if (!$isNull(ctx.ClientControl) &&
ctx.ListDataJSONGroupsKey = "ResultTables";
var $noResults = Srch.ContentBySearch.getControlTemplateEncodedNoResultsMessage(ctx.ClientControl);
var noResultsClassName = "ms-srch-result-noResults";
var ListRenderRenderWrapper = function(itemRenderResult, inCtx, tpl)
var iStr = ;
ctx['ItemRenderWrapper'] = ListRenderRenderWrapper;
_#= ctx.RenderGroups(ctx) =#_
<div class="_#= noResultsClassName =#_">_#= $noResults =#_</div>
You can use jQuery within your display templates, you must include the jQuery library in your Script Block. Also because jQuery is used to using the # symbol, which is also required in the templates, to reference an ID Selector using jquery it is recommend to use the following code;
var containerQueryId = ‘#’ + ‘_#= containerId =#_’;
$(‘_#= containerQueryId =#_’);
Display Template for Content Search WebPart.
When creating a Display Template it is recommend to copy an existing HTML display template that is similar to the one you wish to create. Once you have copied it and renamed, and reconfigured it, and saved it back to the Master Page Gallery, you will find a corresponding .js file automatically created for you.
By browsing to your publishing site, in the upper right corner of the page, choose Settings, and then choose Design Manager.
In Design Manager, in the left panel area, choose Edit Display Templtes. Your HTML file will now appear with a Status column that will either show “Warnings and Errors” or “Conversion successful”. Please note that Approval Status is set to “Draft” and you need to publish any files added to the Master Page Gallery from within the Master Page Gallery. Once you have converted the template successfully, but going to your web page with eth Content Search Web Part on, when editing you will now see your new template in the corresponding Display Template Control or item.