How to output ${expression} in Freemarker without it being interpreted?

This should print ${person.name}: ${r”${person.name}”} From the freemarker docs A special kind of string literals is the raw string literals. In raw string literals, backslash and ${ have no special meaning, they are considered as plain characters. To indicate that a string literal is a raw string literal, you have to put an r directly … Read more

Passing a List of Objects to Freemarker and then Looping

Is “jobs” really a collection? Please post a snippet of code where you are creating and processing your template. I just wrote a quick test to check: public void testFreeMarker() throws Exception { List<Invoice> invoices = Arrays.asList( new Invoice( “note1”, “amount1” ), new Invoice( “note2”, “amount2” ) ); Map<String, Object> root = new HashMap<String, Object>(); … Read more

How do I call java methods on an object from a FreeMarker template?

FreeMarker allows invoking methods that were made available through the model from within expressions. Assuming your object has been exposed as myBean you can invoke the method as follows: <#list myBean.getunits(“myType”) as unit> do stuff with ${unit} </#list> You don’t have to use <#list>, of course, it’s just there as an example since your method … Read more

Use ternary operator in freemarker?

If you’re using freemarker 2.3.23 or newer, you can use the then built-in: <a href=”https://stackoverflow.com/questions/17539686/${a?then(“a.htm’,’b.html’)}” target=”${openTarget}”> If you’re using an older version of freemarker, you can use instead the string built-in: <a href=”https://stackoverflow.com/questions/17539686/${a?string(“a.htm’,’b.html’)}” target=”${openTarget}”> When applied to a boolean, the string built-in will act as a ternary operator.

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