Friday 3 July 2020

FreeMakrer: assign Directive: Define variables

‘assign’ directive is used to define variables.

 

Syntax

<#assign name1=value1 name2=value2 ... nameN=valueN>

 

Naming conventions

a.   Variable name can only contain letters, digits, underline (_), dollar ($), at-sign (@).

b.   First character of a variable name can't be an ASCII digit (0-9).

c.    Variable name can also contain minus (-), dot (.), and colon (:)

 

Example

<#assign empDetails={ "id" : 1, "name": "krishna", "city": "Bangalore" } >

<#assign items = [1, 2, 3, 4]>

<#assign a=10, b=11, name="Krishna">

 

Find the below working application.

 

Step 1: Create assignDirective.ftl file under src/main/resources/templates folder.

 

assignDirective.ftl
<#assign empDetails={ "id" : 1, "name": "krishna", "city": "Bangalore" } >

<#list empDetails as key, value> 
${key} = ${value} 
</#list>


<#assign items = [1, 2, 3, 4]>
<#list items as item> 
Value of item is ${item}
</#list>

<#assign a=10, b=11, name="Krishna">

Value of a is ${a}
Value of b is ${b}
Value of name is ${name}

Step 2: Define ‘FreeMarkerUtil’ class that takes model object and template file as input and merge them.

 

FreeMarkerUtil.java
package com.sample.app.util;

import java.io.StringWriter;
import java.util.Locale;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

public class FreeMarkerUtil {

	private static final Configuration FREE_MARKER_CONFIGURATION = new Configuration(Configuration.VERSION_2_3_30);

	static {
		FREE_MARKER_CONFIGURATION.setClassForTemplateLoading(FreeMarkerUtil.class, "/templates/");
		FREE_MARKER_CONFIGURATION.setDefaultEncoding("UTF-8");
		FREE_MARKER_CONFIGURATION.setLocale(Locale.US);
		FREE_MARKER_CONFIGURATION.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
		FREE_MARKER_CONFIGURATION.setFallbackOnNullLoopVariable(false);
	}

	public static StringWriter mergeModelAndTemplate(Object modelObject, String ftlFile) throws Exception {
		StringWriter stringWriter = new StringWriter();

		Template template = FREE_MARKER_CONFIGURATION.getTemplate(ftlFile);

		template.process(modelObject, stringWriter);

		return stringWriter;
	}

}

Step 3: Define AssignDirectivePopulator.

 

AssignDirectivePopulator.java
package com.sample.app;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import com.sample.app.util.FreeMarkerUtil;

public class AssignDirectivePopulator {
	public static void main(String args[]) throws Exception {

		Map<String, Object> modelObject = new HashMap<String, Object>();
		
		StringWriter stringWriter = FreeMarkerUtil.mergeModelAndTemplate(modelObject, "assignDirective.ftl");
		System.out.println(stringWriter.toString().trim());

	}
}

Output

id = 1 
name = krishna 
city = Bangalore 


Value of item is 1
Value of item is 2
Value of item is 3
Value of item is 4


Value of a is 10
Value of b is 11
Value of name is Krishna

Previous                                                    Next                                                    Home

No comments:

Post a Comment