Settings

Settings are named values that influence the behavior of FreeMarker. Examples of settings are: locale, number_format, default_encoding, template_exception_handler. The full list of settings can be found in the Java API documentation of Configuration.setSetting(...) .

The settings coming from the Configuration can be overridden in a Template instance. For example, if you set the locale setting to "en_US" in the configuration, then the locale in all templates that use this configuration will be "en_US", except in templates where the locale was explicitly specified differently (see localization). Thus, the setting values in the Configuration serve as defaults that can be overridden in a per template manner. The value coming from the Configuration instance or Template instance can be further overridden for a single Template.process call. For each such call a freemarker.core.Environment object is created internally that holds the runtime environment of the template processing, including the setting values that were overridden on that level. The values stored there can even be changed during the template processing, so a template can set settings itself, like switching locale at the middle of the ongoing processing.

This can be imagined as 3 layers (Configuration, Template, Environment) of settings, where the topmost layer that contains the value for a certain setting provides the effective value of that setting. For example (settings A to F are just imaginary settings for this example):

Setting A Setting B Setting C Setting D Setting E Setting F
Layer 3: Environment 1 - - 1 - -
Layer 2: Template 2 2 - - 2 -
Layer 1: Configuration 3 3 3 3 - -

The effective value of settings will be: A = 1, B = 2, C = 3, D = 1, E = 2. The F setting is probably null, or it throws exception when you try to get it.

Let's see exactly how to set settings:

To see the list of supported settings and their meaning, please read the following parts of the FreeMarker Java API documentation:

上一章 首页 下一章