As you might already know, data-model + template = output. We
already have a data-model (root
) and a template
(temp
), so to get the output we have to merge them.
This is done by the process
method of the template.
It takes the data-model root and a Writer
as
parameters. It writes the produced output to the
Writer
. For the sake of simplicity here I write to
the standard output:
Writer out = new OutputStreamWriter(System.out); temp.process(root, out);
This will print to your terminal the output you have seen in the first example of the Template Author's Guide.
Java I/O related notes: Depending on what out
is, you may need to ensure that out.close()
is
called. This is typically needed when out
writes
into a file that was opened to store the output of the template. In
other times, like in typical Web applications, you must
not close out
. FreeMarker
calls out.flush()
after a successful template
execution (but tis can be disabled in
Configuration
), so you don't need to worry about
that.
Note that once you have obtained a Template
instance, you can merge it with different data-models for unlimited
times (Template
instances are stateless). Also, the
test.ftlh
file is accessed only while the
Template
instance is created, not when you call the
process method.