How to escape a # in velocity
this: #[[ ## ]]# will yield: ## anything within #[[ … ]]# is unparsed.
this: #[[ ## ]]# will yield: ## anything within #[[ … ]]# is unparsed.
Great question – I solved my issue today as follows using Ecilpse: Put your template in the same folder hierarchy as your source code (not in a separate folder hierarchy even if you include it in the build path) as below: In your code simply use the following lines of code (assuming you just want … Read more
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
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
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
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);