2

I have an application where in I am trying to create a velocity template repository which will help me centralise all my email templates and will allow me to create a communication hub. All templates will be called at runtime and populates with data via services.

My problem is that I need to provide users with optional and compulsory params list when they define the template inputs for the velocity template.

Is there a way to read the tokens/tags from the velocity template file and extract them??

Like I want a list of tokens $name.address.streetName to be available to me from .vm file.

I do not want to go for Regex .

I do not have to cache or reuse them , its just going to be a one time read and store the default,compulsory & optional params in the database.

I am following these patterns : http://kickjava.com/src/org/apache/velocity/test/view/TemplateNodeView.java.htm

How to use String as Velocity Template?

Please advice.

Community
  • 1
  • 1
kumarn
  • 111
  • 8

1 Answers1

1

I got it working like this

            RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();            
        StringReader reader = new StringReader(String velocityTemplateBodu);
        SimpleNode node = runtimeServices.parse(reader, "dummyOne.vm");


        for(int i=0; i<node.jjtGetNumChildren();i++){
            if(node.jjtGetChild(i) instanceof org.apache.velocity.runtime.parser.node.ASTReference ){
                System.out.println("Node -----------------"+i +"---"+node.jjtGetChild(i).literal());
            }
        }

Using SimpleNode class you get all the nodes on the .vm file.

The Nodes are read using javaCC as ASTReference and ASTText (both extend SimpleNode). To get the tokens you need to get the ASTReference and to get HTML text use the ASTText.

kumarn
  • 111
  • 8