How to pass String as input in FreeMarker?
freemarker, java
Solution
You can pass your template to the Template constructor with a StringReader:
// Get your template as a String from the DB
String template = getTemplateFromDatabase();
Map<String, Object> model = getModel();
Configuration cfg = new Configuration();
cfg.setObjectWrapper(new DefaultObjectWrapper());
Template t = new Template("templateName", new StringReader(template), cfg);
Writer out = new StringWriter();
t.process(model, out);
String transformedTemplate = out.toString();
Problem
All the templates are stored in database. And i have to fetch contents of a template from database and marked up with freemarker. The end output will be rendered in a textbox. But, i am not finding any methodology by which i can send string instead of file name. Please suggest.