outputformat

Synopsis

<#outputformat formatName>
  ...
</#outputFormat>

Where:

Camel case name variant: outputFormat

Note:

outputformat exists since FreeMarker 2.3.24.

Description

Sets the output format to the specified one, inside the nested block. At the end of the block, the earlier output format is restored.

This directive only has effect on the section that is literally (as in the text editor) inside the nested bock, not on the parts that are called/included from there.

Example:

Template
<#ftl output_format="XML">
XML escaping: ${"&{}"}
<#outputformat "RTF">
  RTF escaping: ${"&{}"}
</#outputformat>
<#outputformat "plainText">
  No escsaping: ${"&{}"}
</#outputformat>
XML escsaping: ${"&{}"}
Output
XML escsaping: &amp;{}
  RTF escaping: &\{\}
  No escsaping: &{}
XML escsaping: &amp;{}

Combined (nested) output formats

When outputformat-s are nested into each other, normally, only the innermost output format will count. For example:

Template
<#ftl output_format="XML">
${"'{}"}
<#outputformat "HTML">
  ${"'{}"}
  <#outputformat "RTF">
    ${"'{}"}
  </#outputformat>
</#outputformat>
Output
&apos;{}
  &#39;{}
    '\{\}

But sometimes you want all enclosing output format escaping to be applied at once. In that case the 2nd ${...} above should be escaped with "HTML" and then with "XML", and the 3rd ${...} should be escaped with "RTF" and then with "HTML" and then with "XML". These are called combined output formats, and can be referred by names like "XML{HTML}" and "XML{HTLM{RTF}}", respectively. We could use these names in the earlier two outputformat calls, however, there's a shorthand where you inherit the part outside the {...} from the enclosing output format:

Template
<#ftl outputFormat="XML">
${"'{}"}
<#outputFormat "{HTML}"><#-- Same as "XML{HTML}" -->
  ${"'{}"}
  <#outputFormat '{RTF}'><#-- Same as "XML{HTML{RTF}}" -->
    ${"'{}"}
  </#outputFormat>
</#outputFormat>
Output
&apos;{}
  &amp;#39;{}
    &amp;#39;\{\}