Class JMustacheRenderer

java.lang.Object
io.jstach.opt.jmustache.JMustacheRenderer
All Implemented Interfaces:
JStachioExtension, JStachioExtensionProvider, JStachioFilter

@MetaInfServices(JStachioExtension.class) public class JMustacheRenderer extends Object
Use JMustache instead of JStachio for rendering. The idea of this extension is to allow you to edit Mustache templates in real time without waiting for the compile reload cycle.

You are probably asking yourself why do I need JMustache if I have JStachio? Unfortunately JStachio needs the annotation processor to run every time a template is changed!. While there are incremental compilers like Eclipse that do support incrementally compiling annotations they are often not triggered via editing resources. Furthermore incremental compilation often just doesn't work.

Enter JMustache. Through reflection you can edit your templates while an application is running. Luckily JMustache and JStachio are almost entirely compatible especially through this extension which configures JMustache to act like JStachio. Even JStacheLambda will work.

The only major compatibility issue is that JMustache currently does not support mustache inheritance (parents and blocks)!

If this extension is enabled which it is by default if the ServiceLoader finds it JMustache will be used when a runtime filtered rendering call is made (see JStachio).

How this works is this extension is a filter that checks to see if the statically generated renderer (template) can render and that its template is up-to-date. If it is not then JMustache will use the template meta data to construct its own template and then execute it. In some cases the annotation processor does not even have to run for this to work (see Templates.getInfoByReflection(Class).

Strongly recommended you disable this in production via JSTACHIO_JMUSTACHE_DISABLE or use

Author:
agentgt
See Also:
  • Field Details

  • Constructor Details

  • Method Details

    • use

      public JMustacheRenderer use(boolean flag)
      Enables JMustache
      Parameters:
      flag - true enables
      Returns:
      return this for builder like config
    • prefix

      public JMustacheRenderer prefix(@Nullable String prefix)
      A prefix to add to the output to know that JMustache is being used.
      Parameters:
      prefix - string to prefix output
      Returns:
      return this for builder like config
    • suffix

      public JMustacheRenderer suffix(@Nullable String suffix)
      A suffix to append to the output to know that JMustache is being used.
      Parameters:
      suffix - string to suffix output
      Returns:
      return this for builder like config
    • sourcePath

      public JMustacheRenderer sourcePath(String sourcePath)
      Sets the relative to the project sourcePath for runtime lookup of templates. By default is src/main/resources.
      Parameters:
      sourcePath - by default is src/main/resources
      Returns:
      sourcePath should not be null
    • log

      protected void log(boolean flag)
      Log plugin on reload.
      Parameters:
      flag - true is if extension is enabled.
    • log

      protected void log(TemplateInfo template)
      Log template execution through jmustache
      Parameters:
      template - template to execute.
    • init

      public void init(JStachioConfig config)
      Description copied from interface: JStachioExtensionProvider
      Called before the extensions are used but after JStachioExtensionProvider.provideConfig(). See JStachioExtensionProvider.provideConfig() on how the config is consolidated to a single config.
      Parameters:
      config - the composite config never null
    • execute

      protected boolean execute(Object context, Appendable a, TemplateInfo template, boolean broken) throws IOException
      Execute the template engine. If the engine chooses not to participate false should be returned.
      Parameters:
      context - the model
      a - the appendable to write to
      template - template info
      broken - whether or no the previous filter (usually jstachio itself) is broken.
      Returns:
      true if the engine has written to the appendable
      Throws:
      IOException - error writing to the appendable
    • filter

      Description copied from interface: JStachioFilter
      Advises or filters a previously created filter.
      Specified by:
      filter in interface JStachioFilter
      Parameters:
      template - info about the template
      previous - the function returned early in the chain.
      Returns:
      an advised render function or often the previous render function if no advise is needed.
    • provideFilter

      public final @NonNull JStachioFilter provideFilter()
      Provide a filter or not. The final filter is a composite and becomes a filter chain. The implementation is a filter and provides itself.
      Specified by:
      provideFilter in interface JStachioExtensionProvider
      Returns:
      filter if this service provider provies one or null