In Freemarker templates we can use the escape directive to automatically apply an escaping to all interpolations inside the included block:
<#escape x as x?html>
<#-- name is escaped as html -->
Hallo, ${name}
</#escape>
Is there a way to programmatically achieve a similar effect, defining a default escape applied to all interpolations in the template, including those outside escape directives?
Thanks.
To elaborate on Attila's answer: you can use a class like this one and then wrap your template loader like this:
If you don't include linebreaks in the added parts you don't get the line numbering problem. You can't use the <#ftl>/[#ftl] with this approach, though.
The suggested TemplateLoaders in the links need a little tweaking if you are using <#include parse=false .../> to include for example HTML in your templates.
Also, you need to copy spring.ftl and use your own copy with the <#ftl ..> directive on top removed like Tom said.
The following works well, although a bit rough (using guava over commons-io)
You don't actually need a WrappingReader to add the escapes. You can just create a decorator around any TemplateLoader, read in the template into a String, wrap the template text in escapes and then return a StringReader that reads the resulting String. To see how that's done take a look here. The only gotcha that I've found is that if you use this approach and include the spring.ftl macros from the classpath they will blow up since they have a <#ftl> declaration at the very top. You can however simply copy spring.ftl into your template path and remove the declaration (and all the escaping directives since you will be escaping by default).
Since 2.3.24 each template has an associated
freemarker.core.OutputFormat
object, which specifies if and how${...}
(and#{...}
) is escaped.OuputFormat
for HTML, XML and RTF are provided out of the box, but you can also define your own formats. When the selectedOutputFormat
escapes by default, then you can prevent escaping explicitly like${foo?no_esc}
.There are several ways of associating templates with the
OutputFormat
you want. For HTML and XML escaping, the recommended way is setting therecognize_standard_file_extensions
configuration setting totrue
, then usingftlh
file extension for HTML, andftlx
file extension for XML templates. You can also associateOutputFormat
-s to templates based on arbitrary template name (template path) patterns, using thetemplate_configurers
setting. Last not least, you can just set the default output format globally likeconfiguration.setOutputFormat(HTMLOutputFormat.INSTANCE)
. You can also override the output format at the top of the template as<#ftl output_format='HTML'>
, though it should be used rarely.Related documentation pages: http://freemarker.org/docs/dgui_misc_autoescaping.html, http://freemarker.org/docs/pgui_config_outputformatsautoesc.html
There is a solution, although it's not entirely trivial. You can create a special TemplateLoader that wraps other template loaders, and injects <#escape x as x?html> in the prolog of the template source text, and adds as the epilogue of it.
Obvious drawbacks: - column numbers in first line will be thrown off - if your template starts with <#ftl> declaration, you need to insert <#escape> after it.