m.antkowicz
m.antkowicz

Reputation: 13581

Define Freemarker EOL format (\n instead of \r\n)

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

Is there any global way to set the EOL format without manipulating each .ftl manually and separately?

Upvotes: 1

Views: 1183

Answers (1)

ddekany
ddekany

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

Related Questions