Question

Freemarker has the ability to do text escaping using something like this:

<#escape x as x?html>
Foo: ${someVal}
Bar: ${someOtherVal}
</#escape>

xml, xhtml, and html are all built in escapers. Is there a way to register a custom written escaper? I want to generate CSV and have each individual element escaped and that seems like a good mechanism.

I'm trying to do this in Struts 2 if that matters as well.

Was it helpful?

Solution

You seem to be confusing two concepts here. ?xml, ?xhtml and ?html are string built-ins.
<#escape> OTOH is syntax sugar to save you from typing the same expression over and over again. It can be used with any expression, it's not limited to built-ins.

That said, there's unfortunately no built-in for csv string escaping and there's no way to write your own without modifying FreeMarker source (though if you do want to go this way it's pretty straightforward - take a look at freemarker.core.BuiltIn). Perhaps you can by with ?replace using regex or just write / expose an appropriate method and invoke it in your template.

OTHER TIPS

The Javadoc for HtmlEscaper indicates how to instantiate/register that in code (see the header), so I suspect if you implement your own TemplateTransformModel, and register it in a similar fashion then that should work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top