Apache Velocity has if condition like all other programming language. Find below syntax of #if in velocity.
Single if statement.
#if
#end
If with ElseIf.
#if
#elseif
#end
Find below example template for more details.
condition.vm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#if($i == 0) i is zero #end #set($i = 1) #if(!($i == 0)) i is non zero #end #if($i == 2) i is two #elseif ($i == 1) i is one #end #set($name = "omt") #if($name == "omt") name is omt #end #if($name == "lab" || $i == 1) $name OR $i #end #if($name == "lab" || $i == 2) $name OR $i #end #if($name == "omt" && $i == 1) $name AND $i #end |
Find below code sample.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.omt.velocity.example; import java.io.StringWriter; import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; public class ConditionVelocityExample { public static void main(String[] args) { Velocity.init(); VelocityContext context = new VelocityContext(); context.put("i", 0); Template t = Velocity.getTemplate("condition.vm"); StringWriter sw = new StringWriter(); t.merge(context, sw); System.out.println(sw.toString()); } } |
Output :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
i is zero i is non zero i is one name is omt omt OR 1 omt AND 1 |