Reputation: 13581
In my Spring application I'm using Freemarker to generate export file - simple plain text with header and loop <#list></#list>
to render list of items.
@Bean
public freemarker.template.Configuration freemarkerConfiguration() throws IOException, TemplateException {
return new FreeMarkerConfigurationFactoryBean().createConfiguration();
}
// ...
Template template = getFreemarkerTemplate();
Writer fileWriter = getFileWriter(outputFile);
template.process(getExportData(), writer);
// flush and close writer, some additional business logic
I would like to set the format of the End Of Line from current Windows \r\n
(CRLF) to Unix \n
(LF)
What I actually tried but don't like or is not working
<#rt>${'\n'}
at the end of every line - it's just polluting my code and developer must remember to add this in case of template change<#assign str = str?replace(...)
- seems to not be possible since this replace is working only on variables, not current whole file and I did not found way to render template into variable (I could create then wrapper changing EOL, but to be honest it also doesn't look like a good solutions)Is there any global way to set the EOL format without manipulating each .ftl manually and separately?
Upvotes: 1
Views: 1183
Reputation: 31162
There's no such configuration option (as of 2.3.29). FreeMarker just outputs the static text part of templates as is. Though you didn't like that approach, I believe the best is to do this in the Writer
that you pass to FreeMarker. The overhead of that won't matter at all in most applications.
The other option that avoids runtime overhead is doing the same in a custom TemplateLoader
that wraps another TemplateLoader
. However, the issue with such solutions is that line-breaks sometimes come from outside the templates (i.e., from the data-model), and I think users will expect those to be normalized as well.
So, even if it was a core FreeMarker feature (I guess it could be), it probably would do this on-the-fly, like you can with a custom Writer
.
Upvotes: 1