Sunday 14 June 2020

FreeMarker: if directive

Using if directive, you can conditionally skip some section of the template.

 

Syntax

<#if condition>

  ...

  ...

</#if>

 

For example, I would like to greet user with Mr. if user is Male and Madam, if user is female.

 

greet.ftl

Welcome <#if user.gender == "Male">Mr.</#if> <#if user.gender == "Female">Madam.</#if> ${user.name}

In the above example, user.gender in the #if directive is a variable. In general unquoted words inside directives or interpolations are treated as references to variables.

 

== operator is used to check the value on left hand side is equal to right hand side or not. != is used to check for inequality.

 

Step 1: Define model class GreetUser.

 

GreetUser.java

package com.sample.app.templates.model;

public class GreetUser {
	private String name;
	private String gender;

	public GreetUser(String user, String gender) {
		super();
		this.name = user;
		this.gender = gender;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

}

Step 2: Define ‘FreeMarkerUtil’ class that take model class 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);
	}

	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 GreetUserPopulator.java

 

GreetUserPopulator.java

package com.sample.app;

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

import com.sample.app.templates.model.GreetUser;
import com.sample.app.util.FreeMarkerUtil;

public class GreetUserPopulator {
	public static void main(String args[]) throws Exception {
		Map<String, Object> modelObject = new HashMap<String, Object>();

		modelObject.put("user", new GreetUser("Krishna", "Male"));

		StringWriter stringWriter = FreeMarkerUtil.mergeModelAndTemplate(modelObject, "greet.ftl");
		System.out.println(stringWriter);
		
		modelObject.put("user", new GreetUser("Sailu", "Female"));
		stringWriter = FreeMarkerUtil.mergeModelAndTemplate(modelObject, "greet.ftl");
		System.out.println(stringWriter);
	}

}

Output

Welcome Mr.  Krishna

Welcome  Madam. Sailu




Previous                                                    Next                                                    Home

No comments:

Post a Comment