Create the template

Templates are represented by freemarker.template.Template instances. Typically you obtain a Template instance from a Configuration instance. A Configuration instance is a central place to store some application level settings; also it deals with creation and managing of Template instances.

First you need a Configuration instance and adjust its settings. Probably you will do it only once at the beginning of the application (possibly servlet) life-cycle. Note that you can typically just use the default configuration object. (FreeMarker also supports the presence of multiple configuration objects, but the use of the default singleton is probably more typical usage.)

Configuration cfg = Configuration.getDefaultConfiguration();
// Specify the data source where the template files come from.
// Here I set a file directory for it:
cfg.setDirectoryForTemplateLoading(
        new File("/where/you/store/templates"));  

From this point, whenever you need a template instance you can get it with the getTemplate method. Store the example template in the test.ftl file of the previously set directory, then you can do this:

Template temp = cfg.getTemplate("test.ftl");  

When you call this, it will create a Template instance corresponds to test.ftl, by reading /where/you/store/templates/test.ftl and parsing (compile) it. The Template instance stores the template in the parsed form, and not as text.

Configuration caches Template instances, so when you get test.ftl again, it probably will not create new Template instance (thus doesn't read and parse the file), just returns the same instance as for the first time.


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