Shared variables

Shared variables are variables that are defined for all templates. You can add shared variables to the configuration with the setSharedVariable methods:

Configuration cfg = Configuration.getDefaultConfiguration();
...
cfg.setSharedVariable("to_upper", new UpperCaseTransform());
cfg.setSharedVariable("company", "Foo Inc.");  // Using ObjectWrapper.DEFAULT_WRAPPER  

In all templates that use this configuration, a transform with name to_upper and a string with name company will be visible in the data model root, so you don't have to add them to the root hash again and again. A variable in the root object that you pass to the Template.process will hide the shared variable with the same name.

Warning

Never use TemplateModel implementation that is not thread-safe for shared variables, if the configuration is used by multiple threads! This is the typical situation for Servlet based Web sites.

The set of shared variables is initially (i.e. for a new Configuration instance) not empty. It contains the following transforms:

name

class

capture_output

freemarker.template.utility.CaptureOutput

compress

freemarker.template.utility.StandardCompress

html_escape

freemarker.template.utility.HtmlEscape

normalize_newlines

freemarker.template.utility.NormalizeNewlines

xml_escape

freemarker.template.utility.XmlEscape


Page generated: 2004-06-15 22:17:59 GMT FreeMarker Manual -- For FreeMarker 2.3