Accessing collection.cfg settings from Freemarker

Managed by | Updated .

Background

This article explains how to use the values set in collection configuration from a Freemarker template. The collection configuration object in Freemarker contains all the collection's configuration values sourced from the collection.cfg (including defaults) and global.cfg.

Process

The collection configuration settings are stored within a hidden data model object.

Collection configuration parameters can be used in Freemarker templates by accessing this object:

collection.configuration.value("CONFIGURATION_KEY")
# Can also be used like this:
question.collection.configuration.value("CONFIGURATION_KEY")

where CONFIGURATION_KEY is a key from collection.cfg or global.cfg. This call has the ability to see all user defined parameters found in the collection.cfg file and all default values. The funnelback_classic.ftl template provides a macro that can be used to read and display a collection.cfgparameter for the current collection being searched. The following is the macro body:

<#macro cfg>
 <#compress>
  <#local key><#nested></#local>
  <#if key?exists && key!="" && question.collection.configuration.value(key)?exists>
   ${question.collection.configuration.value(key)}
  </#if>
 </#compress>
</#macro> 

The macro can then called in the template when you need to use the value. The call would look similar to this:

<@s.cfg>name_of_parameter</@s.cfg>

Examples

Example 1. cfg macro

Use the <@s.cfg> macro to get the parameter to setup auto-completion:

<#import "/web/templates/modernui/funnelback_classic.ftl" as s/>
.......
<script type="text/javascript">
//Query completion setup
jQuery(function()  {
  jQuery("#search").fbcompletion({
    'enabled'   : 'enabled',
    'collection': 'collection-name',
    'program'   : '/s/search.html',
    'format'    : 'extended'
    'alpha'     : '<@s.cfg>auto-completion.alpha</@s.cfg>,
    'show'      : '<@s.cfg>auto-completion.show</@s.cfg>, 
    'sort'      : '<@s.cfg>auto-completion.sort</@s,cfg>,
    'length'    : '<@s.cfg>auto-completion.length</@s.cfg>,
    'delay'     : '<@s.cfg>auto-completion.delay</@s,cfg>,
  });
});
</script>   

Example 2. Directly access the data model configuration object

Access the data model hidden object with code similar to:

<#if question?exists && question.collection?exists 
  && question.collection.configuration.value("spelling_enabled")?exists
  && is_enabled(question.collection.configuration.value("spelling_enabled")) >
  <#--- Insert code for when configuration parameter exists and is enabled --->
</#if>

Example 3. Print a link to email the search administrator.

In the Freemarker template:

<p>Contact the search administrator: <a href="mailto:${question.collection.configuration.value("admin_email")?url}">${question.collection.configuration.value("admin_email")}</a></p>

would result in the following being printed out by the template:

<p>Contact the search administrator: <a href="mailto:webmaster@site.com">mailto:webmaster@site.com</a></p>
Was this artcle helpful?

Tags
Type: Keywords:
Features: Frontend > Templating Frontend > Modern UI