How to Parse Templates in FreeMarker

Java Code w/ Cache

import static java.lang.System.out;

import java.io.File;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import freemarker.cache.FileTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;

public final class FreeMarkerSample {

  public static void main(final String[] args) throws Exception {
    final Configuration config = new Configuration(); // uses MruCacheStorage by default

    config.setTemplateLoader(new FileTemplateLoader(new File("C:\\temp")));

    final Template template = config.getTemplate("sample.ftl");

    final Map variableMap = new HashMap();

    variableMap.put("blogName", "Simples Assim"); // may be any Java object

    final Writer writer = new StringWriter();

    template.process(variableMap, writer);

    out.println(writer.toString());
  }

}

Java Code w/o Cache

import static java.lang.System.out;

import java.io.FileReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;

public final class FreeMarkerSample {

  public static void main(final String[] args) throws Exception {
    final Template template = new Template(null, new FileReader("C:\\temp\\sample.ftl"), new Configuration());

    final Map variableMap = new HashMap();

    variableMap.put("blogName", "Simples Assim");

    final Writer writer = new StringWriter();

    template.process(variableMap, writer);

    out.println(writer.toString());
  }

}

Template

{blogName}

http://freemarker.sourceforge.net/docs/api/freemarker/template/Configuration.html

http://freemarker.sourceforge.net/docs/api/freemarker/cache/MruCacheStorage.html

http://freemarker.sourceforge.net/docs/api/freemarker/template/Template.html

2 thoughts on “How to Parse Templates in FreeMarker

  1. Some notes:

    1. The most typical use-case of `new Template(…)` is when you don’t have a storage from which you could load templates by name (like if the template is generated on the fly). Thus `new Template` used with FileReader can be confusing for the readers… but anyway I see the point.

    2. For the sake of better error messages, and maybe due to the resolution of relative #include/#import paths (if there is any in the template), it’s maybe better practice to give some name to the template as the first argument to `new Template(…)`.

    3. Starting from 2.3.17 the recommended way of preventing caching will be the usage of a NullCacheStorage (`cfg.setCacheStorage(new NullCacheStorage)`). Thus the Configuration API can be used as usual.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s