Skip to content

Modern UI util functions

Introduction

There are several helper functions that are available for use within Modern UI hook scripts and Freemarker templates.

Util 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
  • paramName: name of the parameter to be changed
  • value: the new value of the parameter to be set
${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&hellip;</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
${computeQueryStringSignature(qs)}

Examples

Build a list of previously visited queries (search history).

<#assign qsSignature = computeQueryStringSignature(QueryString) />
<#if session.searchHistory?? && (session.searchHistory?size gt 1 || session.searchHistory[0].searchParamsSignature != qsSignature)>
  ...
</#if>

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
  • paramNames: list of names of the facet constraint parameters to remove
${facetScopeRemove(qs, paramNames)}

Freemarker macro

The following macro can also be used to call this function:

<#macro FacetScopeRemove params=[]>
  <#compress>
  <#local qs><#nested></#local>
  ${facetScopeRemove(qs, params)}
  </#compress>
</#macro>

Examples

Remove the f.Location|X facet constraint from the facetScope parameter.

<a href="${question.collection.configuration.value("ui.modern.search_link")!}?<@FacetScopeRemove params=['f.Location|X']>${QueryString}</@FacetScopeRemove>"></a>

filesize

Formats a file size to be displayed in kilobytes (KB). It takes one parameter:

  • value: file size
${filesize(value)}

Examples

Display extension and size of the file.

<#if s.result.fileType!?matches("(doc|docx|ppt|pptx|rtf|xls|xlsx|xlsm|pdf)", "r")>
  <small class="text-muted">${s.result.fileType?upper_case} (${filesize(s.result.fileSize!0)})</small>
</#if>

prettyTime

Utility method for creating human readable timestamps (e.g. "just now", "moments ago", "3 days ago", "within 2 months"). It take one parameter:

  • value: timestamp value

Examples

List search history results and display the date of visit.

<#list session.searchHistory>
  <#items as h>
    <li><a href="?${h.searchParams}">${h.originalQuery!} <small>(${h.totalMatching})</small></a> <span class="text-warning">${prettyTime(h.searchDate)}</span></li>
  </#items>
</#list>

removeParam

Remove a set of parameters from a query string. It takes two parameters:

  • qs: query string
  • paramNames: list of parameters to remove from the string
${removeParam(qs, params)}

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

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)}

Freemarker macro

The following macro can also be used to call this function:

<#macro Truncate length><#compress>
  <#local value><#nested></#local>
  ${truncate(value, length)}
</#compress></#macro>

Examples

Return the first 140 characters of the result title.

<#import "/web/templates/modernui/funnelback_classic.ftl" as s/>
<@s.Truncate length=140>${s.result.title}</@s.Truncate>

urlDecode

Decode a string that has been URL encoded. It takes one parameter:

  • value: the value of URL to be decoded
${urlDecode(value)}

Examples

${urlDecode(h.searchParams)}

value

Get the configuration value for the given property name, 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)}

Examples

Get the value of ui.modern.search_link configuration key.

<form class="quicklinks" action="${question.collection.configuration.value("ui.modern.search_link")}" method="GET">
  ...
</form>

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)}

Examples

Display search history data if session is enabled.

<#if question.collection.configuration.valueAsBoolean("ui.modern.session")>
  ...
</#if>

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)}

Examples

Get the value of the ui.modern.session.search_history.size configuration key.

${question.collection.configuration.valueAsInt("ui.modern.session.search_history.size ")}

See also

top

Funnelback logo
v15.24.0