title: Write freemarker template to String output
body: <p>I would like to have freemarker template output in String.
  I have a freemarker template file <em>commonTemplate.ftl</em>.</p>
  
  <pre><code>&lt;div&gt;
      &lt;div&gt;
          &lt;h1&gt;${userDetails.name}&lt;/h1&gt;
          ${userDetails.birthday}  ${userDetails.id}
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>And Java code which populates model and prints output to console <em>App.java</em>.</p>
  
  <pre><code>public class App {
  
      private Service service = new Service();
  
  
      public void execute() {
          Configuration configuration = prepareConfiguration();
          // Load templates from resources/templatess.
          configuration.setClassForTemplateLoading(App.class, "/templates");
  
          try {
              Template template = configuration.getTemplate("commonTemplate.ftl");
  
              // Console output
              Writer out = new OutputStreamWriter(System.out);
              template.process(prepareData(), out);
              out.flush();
  
          } catch (IOException | TemplateException e) {
              throw new RuntimeException(e);
          }
      }
  
      private Configuration prepareConfiguration() {
          Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
          configuration.setDefaultEncoding("UTF-8");
          configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
          configuration.setLogTemplateExceptions(false);
          return configuration;
      }
  
      private Map&lt;String, Object&gt; prepareData() {
          Model model = service.getModel();
          Map&lt;String, Object&gt; data = new HashMap&lt;String, Object&gt;();
          data.put("userDetails", model.getUserDetails());
          return data;
      }
  }
  </code></pre>
  
  <p>It works for console output.</p>
  
  <pre><code>&lt;div&gt;
      &lt;div&gt;
          &lt;h1&gt;john Doe&lt;/h1&gt;
          1990-01-10T12:11+01:00[Europe/Prague]  1
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>