1
votes

I'm using freemarker template as an template file for my java mail service. I'm trying to make common ftl template and have store the content in the database.

this my sample java model which im binding data with

        model.put("invref",dbModel.getinvoiceRefNumber());
        model.put("myContent", "<p>Hi Your invocie ref number is ${invref}</p>");

I don't want to create new ftl file for each email types rather I want to generate the content at runtime. But it is not binding the inner ftl tag and output I'm getting is

Hi Your invocie ref number is ${invref}

I tried Escaing html by using the below combinations but still no luck

${invref?html}

[#noescape]${invref?html}[/#noescape]

How can I acheive this?

2
Why not just add the value on the model such as: model.put("invref",dbModel.getinvoiceRefNumber()); model.put("myContent", "<p>Hi Your invocie ref number is "+dbModel.getinvoiceRefNumber()+"</p>"); - FDaumas
i'm storing the mycontent in database, I have given a user with UI to modify/adjust the email content... So on the Ui side i have give a note with list of available tags they can use inside the email content - Azeem

2 Answers

1
votes

I will assume that by "not binding" you mean that ${invref} is not resolved in the output. You can solve that like below, but, be aware of the security implications: people who can get values into the DB can then make you execute an arbitrary FreeMarker template. See https://freemarker.apache.org/docs/app_faq.html#faq_template_uploading_security for more. So if that's fine in your case, then:

[@myContent?interpret /]

Note that for most others it will be <@myContent?interpret />, but you are using square-bracket syntax.

Also, I saw you tried to use ?html. That's to HTML-escape a string, so, it has nothing do with interpreting FTL. Also, if you generate HTML e-mails, consider using auto-escaping (I know that you don't use that because then #noescape wouldn't be allowed).

0
votes

Here is how I have achieved this.

Placed the common header and footer content to static ftl files.

Loaded the content of static file as String by using utlitly classFreeMarkerTemplateUtils

   Template header = configuration.getTemplate("header.ftl");
   String headerTemplateAsString =  
   FreeMarkerTemplateUtils.processTemplateIntoString(header, Collections.emptyMap());

StringTemplateLoader allows us to load multiple template string's,include my header and footer content after and before the main content (in my case)

    Template header = configuration.getTemplate("header.ftl");
    String headerTemplateAsString =  
          FreeMarkerTemplateUtils.processTemplateIntoString(header, Collections.emptyMap());

    StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
    stringTemplateLoader.putTemplate("header",headerTemplateAsString);
    stringTemplateLoader.putTemplate("createdTemplate","" +
           "<#include \"header\">"
     + emailContentFromDB );
    configuration.setTemplateLoader(stringTemplateLoader);
    Template template = configuration.getTemplate("createdTemplate");
    String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
    helper.setText(html, true);