Modern UI utility functions
There are several helper functions that are available for use within Freemarker templates and plugin Java code.
Utility functions
changeParam
Utility method used to change a query string parameter. Given a query string, a parameter name and a value it will either add the parameter if it doesn’t exist or replace its existing value with the new one. It takes three parameters:
-
qs: query string (string) -
paramName: name of the parameter to be changed (string) -
value: the new value of the parameter to be set (string)
ensure the query string contains only the set of parameters and values and must not include a leading ?. e.g. collection=example~sp-search&profile=search.
|
${changeParam(qs, paramName, value)}
Examples
Display a "More" link which is used to display more results in the type cluster in contextual navigation.
<li><a rel="more" href="${changeParam(s.category.moreLink, "type_max_clusters", "40")}" class="btn btn-link btn-sm"><small class="glyphicon glyphicon-plus"></small> More…</a></li>
computeQueryStringSignature
Computes a signature for a query string which is independent of the way parameters are encoded, or from their order. It takes one parameter:
-
qs: query string (string)
ensure the query string contains only the set of parameters and values and must not include a leading ?. e.g. collection=example~sp-search&profile=search.
|
${computeQueryStringSignature(qs)}
facetScopeRemove
Utility method used in faceted navigation when building facets breadcrumbs. Used to remove a given list of facet constraints from the facetScope parameter of the query string. It takes two arguments:
-
qs: query string (string) -
paramNames: list of names of the facet constraint parameters to remove (list/sequence)
ensure the query string contains only the set of parameters and values and must not include a leading ?. e.g. collection=example~sp-search&profile=search.
|
${facetScopeRemove(qs, paramNames)}
filesize
Formats a file size to be displayed in kilobytes (KB). It takes one parameter:
-
value: file size
${filesize(value)}
prettyTime
Utility method for creating human readable timestamps (for example, "just now", "moments ago", "3 days ago", "within 2 months"). It take one parameter:
-
value: timestamp value
removeParam
Utility method used to remove one or more query string parameters. It takes two parameters:
-
qs: query string (string) -
paramNames: list of parameters to be removed (list/sequence)
ensure the query string contains only the set of parameters and values and must not include a leading ?. e.g. collection=example~sp-search&profile=search.
|
${removeParam(qs, paramNames)}
Freemarker macro
The following macro can also be used to call this function:
<#macro RemoveParams params=[]>
<#local qs><#nested></#local>
<#compress>
${removeParam(qs, params)}
</#compress>
</#macro>
Examples
Display a "Reset" link which removes the search query and start rank parameters.
<li><a href="${removeParam(QueryString, ["query"]+["start_rank"])}">Reset</a></li>
Remove a custom dateFilter parameter from the faceted navigation clear all facets link.
<li><a href="${response.facetExtras.unselectAllFacetsUrl[1..], ["dateFilter"])}">Clear all</a></li> (1)
| 1 | Note that when using the unselectAllFacets value you must remove the leading ? from the query string. This is performed using the Freemarker string slice function. |
Return a link that strips off all the faceted navigation parameters and the start rank from the query string.
<a href="${question.collection.configuration.value("ui.modern.search_link")!}?<@RemoveParams params=question.selectedCategoryValues?keys + ["start_rank"]">${QueryString}</@RemoveParams>">Clear selected filters</a>
truncate
Truncate a string on word boundaries. It takes two parameters:
-
value: string to be truncated -
length: number of characters to be truncated
${truncate(value, length)}
urlDecode
Decode a string that has been URL encoded. It takes one parameter:
-
value: the value of URL to be decoded
${urlDecode(value)}
currentProfileConfig.get
Get the configuration value for the given property name for the current collection/profile combination or null if it is not set:
-
name: key of property to retrieve
${question.currentProfileConfig.get(name)}
| this function will retrieve the configuration setting regardless of if it is set at the profile or collection-level or set globally. |
configuration.value
Get the configuration value for the given property name for the current collection or null if it is not set. It may take two parameters:
-
name: key of property to retrieve -
defaultValue: if value of property is not defined or is not parseable, return this value (optional)
${question.collection.configuration.value(name)}
this function will not retrieve any settings that are specified in the profile configuration (profile.cfg).
|
configuration.valueAsBoolean
Get the configuration value as a boolean, or the default value if property is not parseable. It may take two parameters:
-
name: key of property to retrieve (required) -
defaultValue: if value of property is not defined or is not parseable, return this value (optional)
${question.collection.configuration.valueAsBoolean(name)}
this function will not retrieve any settings that are specified in the profile configuration (profile.cfg).
|
configuration.valueAsInt
Get the configuration value as a integer, or the default value if property is not parseable. It may take four parameters:
-
name: key of property to retrieve (required) -
defaultValue: if value of property is not defined or is not parseable or if range is defined and value is not within range, return this value (optional) -
minValue: The minimal acceptable value (optional) -
maxValue: The maximum acceptable value (optional)
${question.collection.configuration.valueAsInt(name)}
this function will not retrieve any settings that are specified in the profile configuration (profile.cfg).
|
httpRequest.getHeader
Provides access to HTTP headers that are part of the search request. Takes a single parameter:
-
h: HTTP header name
${httpRequest.getHeader(h)}
Example
Reads a custom HTTP header attribute (X-Funnelback-form) and uses it for a condition within an if statement:
<#ftl encoding="utf-8" />
<#import "/web/templates/modernui/funnelback_classic.ftl" as s/>
<#import "/web/templates/modernui/funnelback.ftl" as fb/>
<html lang="en">
<head>
<#assign mtype=httpRequest.getHeader('X-Funnelback-form')>
<#if mtype == "mobile">
<#include "simple-mobile.ftl">
<#else>
<#include "simple-basic.ftl">
</#if>
</head>
<body></body>
</html>