How to use String as Velocity Template?

There is some overhead parsing template. You might see some performance gain by pre-parsing the template if your template is large and you use it repeatedly. You can do something like this, RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices(); StringReader reader = new StringReader(bufferForYourTemplate); Template template = new Template(); template.setRuntimeServices(runtimeServices); /* * The following line works for Velocity … Read more

Velocity vs. FreeMarker [closed]

The goals for the projects are different. Velocity’s goal is to keep templates as simple as possible, to help maintain a segregation between logic and presentation, so you don’t slide down the slippery slope of sticking code in templates. Sometimes this is the right thing. Of course, sometimes being able to wire complicated logic directly … Read more

Why would I use a templating engine? jsp include and jstl vs tiles, freemarker, velocity, sitemesh

A few arguments for Velocity (I haven’t used Freemarker): Potential to re-use templates outside of a web context, such as in sending emails Velocity’s template language syntax is far simpler than JSP EL or tag libraries Strict separation of view logic from any other sort of logic – no possible option to drop down to … Read more

String replacement in java, similar to a velocity template

Use StringSubstitutor from Apache Commons Text. https://commons.apache.org/proper/commons-text/ It will do it for you (and its open source…) Map<String, String> valuesMap = new HashMap<String, String>(); valuesMap.put(“animal”, “quick brown fox”); valuesMap.put(“target”, “lazy dog”); String templateString = “The ${animal} jumped over the ${target}.”; StringSubstitutor sub = new StringSubstitutor(valuesMap); String resolvedString = sub.replace(templateString);