Knowledge Walls
John Peter
Pune, Maharashtra, India
How to exit from the velocity template language execution using #stop with Example
4412 Views
#stop 
#stop is a vtl exit statement from the vm statements.
ExitUsingStopVTLExample
import java.io.StringWriter;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

public class ExitUsingStopVTLExample {
    public static void main(String args[]){
        VelocityEngine ve = new VelocityEngine();
        ve.init();
         
        Template t = ve.getTemplate("velocitytemplate.vm");
         
        VelocityContext vc = new VelocityContext();
        vc.put("choice", 2);
        
        StringWriter sw = new StringWriter();
        t.merge(vc, sw);
         
        System.out.println(sw);
    }
}
Velocitytemplate.vm 
One
#if ($choice == 1) #stop #end
Two
#if ($choice == 2) #stop #end
Three
#if ($choice == 3) #stop #end
Four
#if ($choice == 4) #stop #end
End
Output 
One
Two
Best Lessons of "Java Apache Velocity Examples"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details