| Refresh | Home EGTry.com

conditional statement in freemarker template: if elseif else

java code to set up model

package egtry.freemarker;

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

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

public class Conditional {

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

		Configuration config=new Configuration();
		config.setClassForTemplateLoading(Conditional.class, "");
		config.setObjectWrapper(new DefaultObjectWrapper());

		Map<String, Object> root=new HashMap<String, Object>();
		root.put("n", 10);
		root.put("name", "Hello");
		Template template=config.getTemplate("if.ftl");
		StringWriter out=new StringWriter();
		template.process(root, out);



template if.ftl

if elseif else 

<#if n &gt; 1>
  n is greater than 1
  <#if n==1>
  n is equal to 1
  <#elseif n==2>
  n is equal to 2
  n is greater than 2
n is not greater than 1


if elseif else 

  n is greater than 1
  n is greater than 2