Accessing a dynamic Freemarker variable

Managed by | Updated .

Background

Accessing a variable that has a dynamic name can be tricky in Freemarker.

The eval built-in function can be used to access the value of a dynamic variable.

Process

Step 1. Construct a variable containing the dynamic name

Construct a variable within the template containing the dynamic variable name.

e.g.

<#-- use this inside a macro -->
<#local numCategories>question.collection.configuration.value("faceted_navigation.${facet.name}.numCategories")</#local>
<#-- use this elsewhere -->
<#assign numCategories>question.collection.configuration.value("faceted_navigation.${facet.name}.numCategories")</#assign>

Step 2. Construct a variable containing the dynamic value

Construct a variable within the template containing the dynamic variable value.

Ensure that the variable assignment handles a null case and sets a default value.

The following sets the maxCategories variable to hold the value of the collection.cfg key faceted_navigation.${facet.name}.numCategories.

e.g.

<#-- use this inside a macro -->
<#local maxCategories=numCategories?eval!""/> 
<#-- use this elsewhere -->
<#assign maxCategories=numCategories?eval!""/> 

Step 3. Comparison against the dynamic varible

To perform comparisons against the dynamic variable test maxCategories

e.g.

<#if maxCategories=="">

Example: read a context-specific collection.cfg value

The example below reads a custom collection.cfg value that sets the maximum number of categories to display for a facet. It then passes this value to another Freemarker macro.

<#-- Read the max number of categories for each facet from config -->
<#-- Set maxCategories to the facet specific config value if it exists, otherwise set it to "".-->
<#local numCategories>question.collection.configuration.value("faceted_navigation.${facet.name}.numCategories")</#local>
<#local maxCategories=numCategories?eval!""/>
<#-- Check if maxCategories = "" and if so set it to either the collection level default from collection.cfg, or a hardcoded system default (20)

     Note: I the above line isn't included in the if statement because there isn't a way to check for the existence of the variable collection.cfg param
-->
<#if maxCategories=="">
  <#if question.collection.configuration.value("faceted_navigation.numCategories")??>

    <#local maxCategories>${question.collection.configuration.value("faceted_navigation.numCategories")}</#local>
  <#else>
    <#local maxCategories="20"/>
  </#if>
</#if>

<@FacetCategory tag="" max=maxCategories?number>
...
Was this artcle helpful?

Tags
Type:
Features: Frontend > Templating Frontend > Freemarker

Comments