Template Syntax

Compressing Whitespace

The code in the function example has a slight problem: most browsers will automatically underline any whitespace between the <a> and </a> tags. FM-Classic provides ways of removing whitespace:

Tag wrapping

In FM-Classic, as with other markup languages, tags can be wrapped to span more than one line. The above example could be re-written as:

<a href="${url}"><if preferences.showImages
   ><img src="${image}" border="0" alt="${alt}"><else
   >${alt}</if
></a>

As you can see, though, the example becomes almost unreadable.

Transform tags

Using transforms provides a cleaner solution. FM-Classic provides a number of transform classes in the freemarker.ext.misc package. Two of these can be used to handle whitespace compression: the CompressWhitespace class, and the LegacyCompress class.

Using the LegacyCompress transformer, the template looks like the following:

<transform utility.legacyCompress>
    <a href="${url}">
    <if preferences.showImages>
        <img src="${image}" border="0" alt="${alt}">
    <else>
        ${alt}
    </if>
    </a>
</transform>

LegacyCompress performs exactly the same compression as the former compress tag in earlier versions of FreeMarker. Unfortunately, the results are the same, too.

<a href="beach.html">Pictureofthebeach</a>

A better idea is to use the CompressWhitespace transformer:

<a href="${url}"><transform utility.compressWhitespace>
    <if preferences.showImages>
        <img src="${image}" border="0" alt="${alt}">
    <else>
        ${alt}
    </if>
</transform></a>

Finally, this gives us the output we expect:

<a href="beach.html">Picture of the beach</a>

Of course, you're not limited to using only these transformers. For more advanced whitespace handling, you could create your own transformer by implementing the TemplateTransformModel interface.