javavelocity

does velocity have a method which returns template by passing String as argument?


I have a requirement in which i have a String which contains file content, i have to create a template of that string using velocity. is there any method in velocity which takes string as input and returns template?


Solution

  • This is how I made a template out of a String:

            RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
    
            StringReader reader = new StringReader(stringValue);
            SimpleNode node = runtimeServices.parse(reader, "Template name");
            Template template = new Template();
            template.setRuntimeServices(runtimeServices);
            template.setData(node);
            template.initDocument();