Search code examples
freemarker

Freemarker trimming right spaces not working


I will be as simple as possible , i have the below template.ftl

B E G L E I T Z E T T E L                                                 Intern
FUER DATEI:                                                  ${content.fileName}
-------------------------------------------------------------------------------- 
BELEGLOSER DATENTRAEGERAUSTAUSCH
ERZ. SYSTEM:                                                   ${content.system}
INITIATOR:                                                  ${content.initiator}
DATEI ID:                                                      ${content.fileID}

And i want to output :

B E G L E I T Z E T T E L                                                 Intern
FUER DATEI:                                                             FileName
--------------------------------------------------------------------------------
BELEGLOSER DATENTRAEGERAUSTAUSCH
ERZ. SYSTEM:                                                             SYSTEMX
INITIATOR:                                                             Initiator
DATEI ID:                                                                 FileID

But what i get instead ... let's say for the above example is :

B E G L E I T Z E T T E L                                                 Intern
FUER DATEI:                                                            FileName
--------------------------------------------------------------------------------
BELEGLOSER DATENTRAEGERAUSTAUSCH
ERZ. SYSTEM:                                                       SYSTEMX
INITIATOR:                                                             Initiator
DATEI ID:                                                              FileID

In other words the column is not properly aligned

How can i fix this problem in FreeMarker :) ?


Solution

  • Here is the answer , i used something not very complex:

    <#assign lineWidth = 80>
    B E G L E I T Z E T T E L                                                 Intern
    <#compress>FUER DATEI:                                                          </#compress>${content.fileName?left_pad(lineWidth-"FUER DATEI:"?length," ")}
    --------------------------------------------------------------------------------
    BELEGLOSER DATENTRAEGERAUSTAUSCH
    <#compress>ERZ. SYSTEM:                                                         </#compress>${content.system?left_pad(lineWidth-"ERZ. SYSTEM:"?length," ")}
    <#compress>INITIATOR:                                                           </#compress>${content.initiator?left_pad(lineWidth-"INITIATOR:"?length," ")}
    <#compress>DATEI ID:                                                            </#compress>${content.fileID?left_pad(lineWidth-"DATEI ID:"?length," ")}
    

    So let's take the 3 first lines of code :

    <#assign lineWidth = 80>
    B E G L E I T Z E T T E L                                                 Intern
    <#compress>FUER DATEI:                                                          </#compress>${content.fileName?left_pad(lineWidth-"FUER DATEI:"?length," ")}
    

    1. Firstly i assign a variable lineWidth which represents the maximum line width of the file.

    2. Then for each line i eat all the spaces using <#compress>

    3. Then i added the value and left added the needed spaces in a way that everything aligns right.

    ----- BOSS LEVEL BELOW - using functions -----

    <#assign lineWidth = 80>
    
    <#function createLine prefix value >
        <#return prefix + value?left_pad(lineWidth - prefix?length," ")>
    </#function>
    
    B E G L E I T Z E T T E L                                                 Intern
    ${createLine("FUER DATEI:",content.fileName)}
    --------------------------------------------------------------------------------
    BELEGLOSER DATENTRAEGERAUSTAUSCH
    ${createLine("ERZ. SYSTEM:",content.system)}
    ${createLine("INITIATOR:",content.initiator)}
    ${createLine("DATEI ID:",content.fileID)}
    ${createLine("SAMMELREFERENZ ID:",content.referenceID)}
    ${createLine("RSTELLUNGSDATUM:",content.creationDate)}