Jason S
Jason S

Reputation: 189826

Freemarker: strip newlines but do NOT compress whitespace?

Is there an easy way to strip newlines but not compress whitespace?

The <@compress> directive does both, which is not what I want.

I have a section in a loop like this:

<#list items as item>
 * <#if something>${something?right_pad(10)}<#else>${something_else?right_pad(10)}</#if><#if another_thing>${more_data?right_pad(20)}<#else>${even_more_data?right_pad(20)}</#if>
</#list>

which makes for some really long lines, and I would greatly prefer doing something like:

<#list items as item>
 * <#if something>
       ${something?right_pad(10)}
   <#else>
       ${something_else?right_pad(10)}
   </#if>
   <#if another_thing>
       ${more_data?right_pad(20)}
   <#else>
       ${even_more_data?right_pad(20)}
   </#if>
</#list>

but it appears there isn't an easy way to disambiguate the spacing here given for clarity, with the spacing I want to output using right_pad.

Upvotes: 5

Views: 5220

Answers (1)

rsutormin
rsutormin

Reputation: 1649

You have at least two options:

1) Use commented out white spaces:

<#list items as item>
 * <#if something><#--
       -->${something?right_pad(10)}<#--
   --><#else><#--
       ${something_else?right_pad(10)}<#--
   --></#if><#--
   --><#if another_thing><#--
       -->${more_data?right_pad(20)}<#--
   --><#else><#--
       -->${even_more_data?right_pad(20)}<#--
   --></#if>
</#list>

2) Use <#lt> (left trim for current line WITHOUT new line symbol) or/and <#rt> (right trim for current line WITH new line symbol)

<#list items as item>
 * <#if something><#rt>
       ${something?right_pad(10)}<#lt><#rt>
   <#else><#lt><#rt>
       ${something_else?right_pad(10)}<#lt><#rt>
   </#if><#lt><#rt>
   <#if another_thing><#lt><#rt>
       ${more_data?right_pad(20)}<#lt><#rt>
   <#else><#lt><#rt>
       ${even_more_data?right_pad(20)}<#lt><#rt>
   </#if><#lt>
</#list>

Check this for details: http://freemarker.org/docs/dgui_misc_whitespace.html

Upvotes: 8

Related Questions