Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass String as input in FreeMarker? [duplicate]

Tags:

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.

like image 691
Shashi Avatar asked Jul 19 '13 12:07

Shashi


People also ask

How do you append strings in FreeMarker?

$( document ). ready(function() { var html = ${itemsToAppendTo} $('. gTA'). append( html ) });

How do you add values on FreeMarker?

Interpolation and concatenation. If you want to insert the value of an expression into a string, you can use ${...} (and the deprecated #{...} ) in string literals. ${...} in string literals behaves similarly as in text sections (so it goes through the same locale sensitive number and date/time formatting).

Which is better Thymeleaf or FreeMarker?

In the pursuit of comparison between FreeMarker , Thymeleaf, Groovy and Mustache, FreeMarker has the upper hand in performance. However, Thymeleaf wins the battle overall.

What does C mean in FreeMarker?

c (when used with numerical value) This built-in converts a number to string for a "computer language" as opposed to for human audience. That is, it formats with the rules that programming languages used to use, which is independent of all the locale and number format settings of FreeMarker.


1 Answers

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();
like image 199
obourgain Avatar answered Oct 16 '22 12:10

obourgain