blob: f14d316bf1a9e0f9fd8fa1eb29afb21022d000ca [file] [log] [blame]
<!doctype html>
<!-- Generated by FreeMarker/Docgen from DocBook -->
<html lang="en" class="page-type-section">
<head prefix="og: http://ogp.me/ns#">
<meta charset="utf-8">
<title>Settings - FreeMarker Manual</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="format-detection" content="telephone=no">
<meta property="og:site_name" content="FreeMarker Manual">
<meta property="og:title" content="Settings">
<meta property="og:locale" content="en_US">
<meta property="og:url" content="http://example.com/pgui_config_settings.html">
<link rel="canonical" href="http://example.com/pgui_config_settings.html">
<link rel="icon" href="favicon.png" type="image/png">
<link rel="stylesheet" type="text/css" href="docgen-resources/docgen.min.css?1594338517553">
</head>
<body itemscope itemtype="https://schema.org/Code">
<meta itemprop="url" content="http://example.com/">
<meta itemprop="name" content="FreeMarker Manual">
<!--[if lte IE 9]>
<div style="background-color: #C00; color: #fff; padding: 12px 24px;">Please use a modern browser to view this website.</div>
<![endif]--><div class="header-top-bg"><div class="site-width header-top"><a class="logo" href="http://example.com" role="banner"> <img itemprop="image" src="logo.png" alt="My Logo">
</a></div></div><div class="header-bottom-bg"><div class="site-width search-row"><a href="index.html" class="navigation-header">FreeMarker Manual</a><div class="navigation-header"></div></div><div class="site-width breadcrumb-row"><ul class="breadcrumb" itemscope itemtype="http://schema.org/BreadcrumbList"><li class="step-0" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="index.html"><span itemprop="name">FreeMarker Manual</span></a></li><li class="step-1" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="pgui.html"><span itemprop="name">Programmer&#39;s Guide</span></a></li><li class="step-2" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="pgui_config.html"><span itemprop="name">The Configuration</span></a></li><li class="step-3" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="pgui_config_settings.html"><span itemprop="name">Settings</span></a></li></ul><div class="bookmarks" title="Bookmarks"><span class="sr-only">Bookmarks:</span><ul class="bookmark-list"><li><a href="alphaidx.html">Index</a></li><li><a href="gloss.html">Glossary</a></li><li><a href="ref.html">Reference</a></li><li><a href="app_faq.html">FAQ</a></li><li><a href="preface.html#test_target">Bőregér</a></li></ul></div></div></div> <div class="main-content site-width">
<div class="content-wrapper no-toc">
<div id="table-of-contents-wrapper" class="col-left">
</div>
<div class="col-right"><div class="page-content"><div class="page-title"><div class="pagers top"><a class="paging-arrow previous" href="pgui_config_sharedvariables.html"><span>Previous</span></a><a class="paging-arrow next" href="pgui_config_templateloading.html"><span>Next</span></a></div><div class="title-wrapper">
<h1 class="content-header header-section1" id="pgui_config_settings" itemprop="headline">Settings</h1>
</div></div><p><strong>Settings</strong> are named values that
influence the behavior of FreeMarker. Examples of settings are:
<code class="inline-code">locale</code>, <code class="inline-code">number_format</code></p><p>Settings stored in <code class="inline-code">Configuration</code> instance can
be overridden in a <code class="inline-code">Template</code> instance. For example
you set <code class="inline-code">&quot;en_US&quot;</code> for the <code class="inline-code">locale</code>
setting in the configuration, then the <code class="inline-code">locale</code> in
all templates that use this configuration will be
<code class="inline-code">&quot;en_US&quot;</code>, except in templates where the locale was
explicitly specified differently (see <a href="ref_directive_include.html#ref_directive_include_localized">localization</a>). Thus,
values in a <code class="inline-code">Configuration</code> serve as defaults that
can be overridden in a per template manner. The value comes from
<code class="inline-code">Configuration</code> instance or
<code class="inline-code">Template</code> instance can be further overridden for a
single <code class="inline-code">Template.process</code> call. For each such call a
<code class="inline-code">freemarker.core.Environment</code> 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
<code class="inline-code">locale</code> at the middle of the output.</p><p>This can be imagined as 3 layers
(<code class="inline-code">Configuration</code>, <code class="inline-code">Template</code>,
<code class="inline-code">Environment</code>) 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):</p> <div class="table-responsive">
<table class="table">
<thead>
<tr>
<th align="left"></th>
<th align="center">Setting A</th>
<th align="center">Setting B</th>
<th align="center">Setting C</th>
<th align="center">Setting D</th>
<th align="center">Setting E</th>
<th align="center">Setting F</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">Layer 3: <code class="inline-code">Environment</code></td>
<td align="center">1</td>
<td align="center">-</td>
<td align="center">-</td>
<td align="center">1</td>
<td align="center">-</td>
<td align="center">-</td>
</tr>
<tr>
<td align="left">Layer 2: <code class="inline-code">Template</code></td>
<td align="center">2</td>
<td align="center">2</td>
<td align="center">-</td>
<td align="center">-</td>
<td align="center">2</td>
<td align="center">-</td>
</tr>
<tr>
<td align="left">Layer 1: <code class="inline-code">Configuration</code></td>
<td align="center">3</td>
<td align="center">3</td>
<td align="center">3</td>
<td align="center">3</td>
<td align="center">-</td>
<td align="center">-</td>
</tr>
</tbody>
</table>
</div>
<p>The effective value of settings will be: A = 1, B = 2, C = 3, D
= 1, E = 2. The F setting is probably <code class="inline-code">null</code>, or it
throws exception when you try to get it.</p><p>Let&#39;s see exactly how to set settings:</p><ul>
<li>
<p><code class="inline-code">Configuration</code> layer: In principle you set
the settings with the setter methods of the
<code class="inline-code">Configuration</code> object, fore example:</p>
<div class="code-wrapper"><pre class="code-block code-unspecified">Configuration myCfg = new Configuration();
myCfg.setLocale(java.util.Locale.ITALY);
myCfg.setNumberFormat(&quot;0.####&quot;);</pre></div>
<p>You do it before you start to actually use the
<code class="inline-code">Configuration</code> object (typically, when you
initialize the application); you should treat the object as
read-only after that.</p>
<p>In practice, in most Web application frameworks you have to
specify the setting in a framework-specific configuration file
that require specifying setting as <code class="inline-code">String</code>
name-value pairs (like in a <code class="inline-code">.properties</code> file).
In that case the authors of the frameworks most probably use the
<code class="inline-code">setSetting(String name, String value)</code> method of
<code class="inline-code">Configuration</code>; see available setting names and
the format of the values in the API doc of
<code class="inline-code">setSetting</code>. Example for Spring
Framework:</p>
<div class="code-wrapper"><pre class="code-block code-unspecified">&lt;bean id=&quot;freemarkerConfig&quot;
class=&quot;org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer&quot;&gt;
&lt;property name=&quot;freemarkerSettings&quot;&gt;
&lt;props&gt;
&lt;prop key=&quot;locale&quot;&gt;it_IT&lt;/prop&gt;
&lt;prop key=&quot;number_format&quot;&gt;0.####&lt;/prop&gt;
&lt;/props&gt;
&lt;/property&gt;
&lt;/bean&gt;</pre></div>
<p>Note that this kind of configuring
(<code class="inline-code">String</code> key-value pairs) is unfortunately
limited compared to directly using the API of
<code class="inline-code">Configuration</code>.</p>
</li>
<li>
<p><code class="inline-code">Template</code> layer: You shouldn&#39;t set
settings here, unless you manage the <code class="inline-code">Template</code>
objects instead of a
<code class="inline-code">freemarker.cache.TemplateCache</code>, in which case
you should set the setting before the <code class="inline-code">Template</code>
object is first used, and then treat the
<code class="inline-code">Template</code> object as read-only.</p>
</li>
<li>
<p><code class="inline-code">Environment </code>layer: There are two ways
doing it:</p>
<ul>
<li>
<p>With Java API: Use the setter methods of the
<code class="inline-code">Environment</code> object. Certainly you want to
do that just before the processing of the template is started,
and then you run into the problem that when you call
<code class="inline-code">myTemplate.process(...)</code> it creates the
<code class="inline-code">Environment</code> object internally and the
immediately processes the template, so you had no chance. The
solution is that this two steps can be separated like
this:</p>
<div class="code-wrapper"><pre class="code-block code-unspecified">Environment env = myTemplate.createProcessingEnvironment(root, out);
env.setLocale(java.util.Locale.ITALY);
env.setNumberFormat(&quot;0.####&quot;);
env.process(); // process the template</pre></div>
</li>
<li>
<p>Directly in the Template: Use the <a href="ref_directive_setting.html#ref.directive.setting"><code>setting</code>
directive</a>, for example:</p>
<div class="code-wrapper"><pre class="code-block code-template">&lt;#setting locale=&quot;it_IT&quot;&gt;
&lt;#setting number_format=&quot;0.####&quot;&gt;</pre></div>
</li>
</ul>
<p>There are no restriction regarding when can you change the
settings in this layer.</p>
</li>
</ul><p>To see the list of supported settings, please read the following
parts of the FreeMarker Java API documentation:</p><ul>
<li>
<p>Setter methods of
<code class="inline-code">freemarker.core.Configurable</code> for the settings
that are in all three layers</p>
</li>
<li>
<p>Setter methods of
<code class="inline-code">freemarker.template.Configuration</code> for the
settings that are available only in the
<code class="inline-code">Configuration</code> layer</p>
</li>
<li>
<p><code class="inline-code">freemarker.core.Configurable.setSetting(String,
String)</code> for settings that are available in all three
layers and are writable with <code class="inline-code">String</code> key-value
pairs.</p>
</li>
<li>
<p><code class="inline-code">freemarker.template.Configuration.setSetting(String,
String)</code> for settings that are available only in the
<code class="inline-code">Configuration</code> layer and are writable with
<code class="inline-code">String</code> key-value pairs.</p>
</li>
</ul><div class="bottom-pagers-wrapper"><div class="pagers bottom"><a class="paging-arrow previous" href="pgui_config_sharedvariables.html"><span>Previous</span></a><a class="paging-arrow next" href="pgui_config_templateloading.html"><span>Next</span></a></div></div></div></div> </div>
</div>
<div class="site-footer"><div class="site-width"><div class="footer-bottom"> <p class="last-generated">
Last generated:
<time itemprop="dateModified" datetime="2020-07-09T23:48:37Z" title="Thursday, July 9, 2020 11:48:37 PM GMT">2020-07-09 23:48:37 GMT</time> </p>
<p class="copyright">
© <span itemprop="copyrightYear">1999</span>–2020
<a itemtype="http://schema.org/Organization" itemprop="copyrightHolder" href="https://apache.org/">The Apache Software Foundation</a> </p>
</div></div></div></body>
</html>