outputformat - Apache FreeMarker Manual

Unsupported web browser - Use a modern browser to view this website!

- Home
- Manual
- Java API

- Contribute
- Report a Bug
- Download

Manual
Previous Next

# outputformat

## Synopsis

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

Where:

- `formatName`: A string constant; can't contain runtime expressions! This is the name of the output format, like `"HTML"`, `"XML"`, etc.; see the table of the predefined output formats here. The referred output format must be known by the `Configuration`, or else a parse-time error will occur. The name can also be like `"outerFormatName{innerFormatName}"`, or `"{innerFormatName}"`; see combined output formats later.

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 block, 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{HTML{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;\{\}

Previous Next