Products Downloads


French version


 

One of the basic principles for implementing the FreeMarker engine is that the data model can only be accessed in read-only mode.

A value from a data model cannot be changed during template execution.

One of the consequences concerns the Hash type: FreeMarker can be used to create a Hash object literally but, once it is created, the object cannot be changed (click here for more details).

 

Example:

<#assign hashVal = {"attr1": "val1",
	"attr2": "val2",
	"attr3": "val3"} />
<#list hashVal as aKey, aVal>...</#list>
${hashVal["attr2"]}
<#-- It is possible to browse the hashVal values or access a hashVal attribute value but it is not easy to change a hashVal attribute value or add/delete an attribute -->
<#-- Inefficient solution for changing an attribute value -->
<#assign attrModified = "attr2" />
<#assign hashVal = hashVal + { attrModified : "val2Modified" }/>
<#-- Inefficient solution for deleting an attribute -->
<#assign hashVal = {"attr1": "val1",
	"attr2": "val2",
	"attr3": "val3"} />
<#assign attrToDelete = "attr2" , tmpHash = {} />
<#list hashVal as key, val>
	<#if key != attrToDelete>
		<#assign tmpHash = tmpHash + {key: val} />
	</#if>
</#list>
<#assign hashVal = tmpHash /> 

 

This function returns a Hash-type object with basic add, change and delete methods. It can take an argument:

  • Either no parameter: the returned Hash object is empty,
  • Or a Hash object: the returned Hash object is a copy. These attributes/values are identical to the Hash object passed as a parameter,
  • a String-type key and a value of any type: the returned Hash type will contain a single attribute, the name of which is the value of the key passed as a parameter and the associated value of which is that passed as a parameter.


Parameters

Hash
/
String

originalHash

key

The returned Hash object contains the originalHash attribute/value pairs.
The returned Hash object contains an attribute called "key"

Optional

All types

value

Attribute value called "key" in the returned object

Optional. Mandatory if the first parameter is String type



For example

<#-- Create empty hash -->
<#assign newHash = hardisCore.createSimpleHash() />
Size of newHash:${newSeq?keys?size}
<#-- Create Hash from another Hash -->
<#assign originalHash = {"attr1": 1, "attr2":true} />
<#assign newHash = hardisCore.createSimpleHash(originalHash) />
Size of newHash:${newSeq?keys?size}
<#-- Create Hash with one sub variable -->
<#assign newHash = hardisCore.createSimpleHash("myKey", "myValue") />
Size of newHash:${newSeq?keys?size}