Class MustacheWhoEmailRenderer

java.lang.Object
org.jwcarman.who.email.MustacheWhoEmailRenderer
All Implemented Interfaces:
WhoEmailRenderer

public final class MustacheWhoEmailRenderer extends Object implements WhoEmailRenderer
Renders who's emails from Mustache templates.

An email whose WhoEmail.template() is sign-in renders sign-in.txt as its text and, when it exists, sign-in.html as its HTML. Each file is looked for in the given template locations, in order, then among who's built-in templates at BUILT_IN_LOCATION; the first that has it wins, so an application or a library replaces one file without touching the others. Partials ({{> footer}}) are found the same way, with the including template's extension.

A template sees three names: email, the email record itself; app, the application-wide values; and extra, the values from WhoEmailModelContributors. A variable the model lacks fails the render; a value that is null renders empty. Text templates are not escaped; HTML templates escape their values.

The subject is the message who.email.<template>.subject in the email's locale, rendered as a text template with the same three names, then put on one line: every run of control characters, line breaks included, becomes one space. The message is read without arguments, so MessageFormat does not touch it, unless the MessageSource always applies it (as Spring's spring.messages.always-use-message-format does), which breaks a subject that uses Mustache tags.

Every template in every location, and each template's subject in Locale.ROOT when it has one, is compiled when the renderer is created, so a template or subject that does not compile fails startup rather than the first send.

  • Field Details

    • BUILT_IN_LOCATION

      public static final String BUILT_IN_LOCATION
      Where feature modules put their built-in templates, on the classpath.
      See Also:
  • Constructor Details

    • MustacheWhoEmailRenderer

      public MustacheWhoEmailRenderer(org.springframework.core.io.support.ResourcePatternResolver resources, List<String> templateLocations, org.springframework.context.MessageSource messages, Map<String,String> app, List<WhoEmailModelContributor> contributors)
      Creates the renderer and compiles every template it can find.
      Parameters:
      resources - finds the templates
      templateLocations - the locations searched before who's own, in order: the application's, then the libraries'; a missing trailing / is added
      messages - supplies the subjects
      app - the application-wide values, seen as app
      contributors - supply the values seen as extra, in order
      Throws:
      IllegalStateException - if a template, or a subject in Locale.ROOT, does not compile
  • Method Details