Associating output formats with templates

The output format associated to a template decides if and what kind of auto-escaping is used in that template (unless the template overrides that with directives). By default, templates have "undefined" output format associated, which does no escaping, and in general gives the behavior that you would expect from a template engine that doesn't care about output formats and escaping. However, if the recognize_standard_file_extensions setting is true (which is the default with the incompatible_improvements setting set to 2.3.24 or higher), templates whose source name ends with ".ftlh" gets "HTML" output format, and those with ".ftlx" get "XML" output format. Using the ftlh and ftlx file extensions is the recommended way of activating HTML and XML auto-escaping. You can also associate output formats to templates based on arbitrary name patterns with the template_configurations setting; see some examples of that below.

There's another a related setting, called auto_escaping_policy, which can be used to disable auto-escaping even if the current output format supports it, or enable auto-escaping even if the format by default doesn't escape (but it supports it). Using this setting rarely advisable, as it's potentially confusing for the template authors. (Instead, escaping can be turned on/off explicitly inside the templates with the auto_esc parameter of the ftl directive, or with the noautoesc and autoesc directives.)

To check if you have configured FreeMarker properly, you can use this template:

<p>Output format: ${.output_format}
<p>Auto-escaping: ${.auto_esc?c}

See the table of predefined output formats here...

Configuration examples:

(You can find some more complex template_configurations setups here...)

上一章 首页 下一章