0 Replies Latest reply on Aug 5, 2013 3:12 PM by dhoffman

    How to import Freemarker macros into mail template XML?

    dhoffman

      I have defined some Freemarker macros in my plugin's .ftl resources for the web interface, and I would like to reuse them in my email templates defined in mail_template_i18n_en.xml.  Is this possible?

       

      What I've tried is

       

      <template name="custom.email.template" displayName="Custom email template">
          <description>Email template presented to the user when sending an email</description>
          <subject><![CDATA[${eventName}]]></subject>
          <textBody><![CDATA[
              (blank)
              ]]>
          </textBody>
          <htmlBody><![CDATA[
              <#include "/plugins/custom-plugin/resources/templates/custom-macros.ftl" as custom />
              <@custom.myMacro />
              ]]>
          </htmlBody>
      </template>
      
      
      
      

       

      and I get this error

      ERROR freemarker.runtime -

      Error reading imported file plugins/custom-plugin/resources/templates/custom-macros.ftl

      The problematic instruction:

      ----------

      ==> import "/plugins/custom-plugin/resources/templates/custom-macros.ftl" as custom [line 71, column 13 custom.email.template.htmlBody:en_US]

      ----------

      Java backtrace for programmers:

      ----------

      freemarker.template.TemplateException: Error reading imported file plugins/custom/resources/templates/custom-macros.ftl

           at freemarker.core.LibraryLoad.accept(LibraryLoad.java:122)

           at freemarker.core.Environment.visit(Environment.java:213)

           at freemarker.core.MixedContent.accept(MixedContent.java:92)

           at freemarker.core.Environment.visit(Environment.java:213)

           at freemarker.core.Environment.process(Environment.java:193)

           at freemarker.template.Template.process(Template.java:237)

           at com.jivesoftware.community.mail.util.TemplateUtil.applyFreemarkerTemplate(TemplateUtil.java:139)

           at com.company.plugin.custom.CustomEmailProcessor.renderTemplate(CustomEmailProcessor.java:154)

           at com.company.plugin.custom.CustomEmailProcessor.getHtmlPreview(CustomEmailProcessor.java:149)

           at com.company.plugin.custom.EmailPreviewAction.getModel(EmailPreviewAction.java:74)

           at com.jivesoftware.community.web.soy.SoyResult.doExecute(SoyResult.java:89)

           ...

           at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)

           at java.lang.Thread.run(Thread.java:662)

      Caused by: java.io.IOException: There was arror loading the template earlier attempt; it is attached as a cause

           at freemarker.cache.TemplateCache.throwLoadFailedException(TemplateCache.java:400)

           at freemarker.cache.TemplateCache.getTemplate(TemplateCache.java:278)

           at freemarker.cache.TemplateCache.getTemplate(TemplateCache.java:235)

           at freemarker.template.Configuration.getTemplate(Configuration.java:487)

           at freemarker.core.Environment.getTemplateForInclusion(Environment.java:1471)

           at freemarker.core.Environment.getTemplateForImporting(Environment.java:1525)

           at freemarker.core.LibraryLoad.accept(LibraryLoad.java:112)

           ... 217 more

      Caused by: java.lang.ArrayIndexOutOfBoundsException: 1

           at com.jivesoftware.community.mail.util.TemplateUtil$JiveLocaleUtilsTemplateLoader.findTemplateSource(TemplateUtil.java:542)

           at freemarker.cache.TemplateCache.acquireTemplateSource(TemplateCache.java:600)

           at freemarker.cache.TemplateCache.findTemplateSource(TemplateCache.java:590)

           at freemarker.cache.TemplateCache.getTemplate(TemplateCache.java:293)

           at freemarker.cache.TemplateCache.getTemplate(TemplateCache.java:235)

           ...

           at com.jivesoftware.community.ext.event.impl.SendEventInvitationProcessor.renderTemplate(SendEventInvitationProcessor.java:154)

           at com.jivesoftware.community.ext.event.impl.SendEventInvitationProcessor.getHtmlPreview(SendEventInvitationProcessor.java:149)

           at com.company.plugin.custom.EmailPreviewAction.getModel(EmailPreviewAction.java:74)

           at com.jivesoftware.community.web.soy.SoyResult.doExecute(SoyResult.java:88)

           ... 207 more

       

      The referenced code that is throwing the Exception is:

              /**
               * Keys must be in the form my.template.name:en_US (template:locale). This key will be used to pull the template
               * source via the {@link TemplateUtil#getTemplate(String, java.util.Locale)} method
               *
               * @param name templateName:locale
               * @return the template source as a {@link String}
               * @throws IOException not thrown
               */
              public Object findTemplateSource(String name) throws IOException {
      
                  String[] tokens = name.split(":");
      
                  String key = tokens[0];
                  Locale locale = LocaleUtils.localeCodeToLocale(tokens[1]);
      
                  return getTemplate(key, locale);
              }
      
      
      
      

       

      name.split(":") is designed to split something like "custom.email.template.htmlBody:en_US".  When attempting to import "/plugins/custom/resources/templates/custom-macros.ftl", tokens[1] as referenced here is out of bounds.

       

      My next experiment will be to render the macro and pass it to the custom.email.template in its model data, then simply add the rendered String if it exists.

       

      Any insight or assistance will be appreciated!