How to read Freemarker Template files from src/main/resources folder?
The root of the classpath is src/main/resources, change the path to configuration.setClassForTemplateLoading(this.getClass(), “/templates/”);
The root of the classpath is src/main/resources, change the path to configuration.setClassForTemplateLoading(this.getClass(), “/templates/”);
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
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
The javadoc is quite explicit: Closing a StringWriter has no effect. And a quick look at the code confirms it: public void close() throws IOException { }
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
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.
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
?? tells if the left hand operand’s value is missing (means it’s Java null or you have an undefined variable there), and gives back false (missing) or true (not missing) accordingly. ?has_content is very much like ??, except it also returns false for a 0-length string or empty FTL sequence (like java.util.List, Java array, etc.) … Read more
Edit: Don’t use this solution with FreeMarker 2.3.25 and up, especially not .get(prop). See other answers. You use the built-in keys function, e.g. this should work: <#list user?keys as prop> ${prop} = ${user.get(prop)} </#list>
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