Class Jinjava

java.lang.Object
com.hubspot.jinjava.Jinjava

public class Jinjava extends Object
The main client API for the Jinjava library, instances of this class can be used to render jinja templates with a given map of context values. Example use:
 Jinjava jinjava = new Jinjava();
 Map<String, Object> context = new HashMap<>();
 context.put("name", "Jared");
 // ...
 String template = "Hello, {{ name }}";
 String renderedTemplate = jinjava.render(template, context);
 
Author:
jstehler
  • Constructor Details

    • Jinjava

      public Jinjava()
      Create a new Jinjava processor instance with the default global config
    • Jinjava

      public Jinjava(JinjavaConfig globalConfig)
      Create a new jinjava processor instance with the specified global config
      Parameters:
      globalConfig - used for all render operations performed by this processor instance
  • Method Details

    • setResourceLocator

      public void setResourceLocator(ResourceLocator resourceLocator)
      Set the object responsible for locating templates referenced in other templates
      Parameters:
      resourceLocator - the locator to use for loading all templates
    • getExpressionFactory

      public javax.el.ExpressionFactory getExpressionFactory()
      Returns:
      The EL factory used to process expressions in templates by this instance.
    • getEagerExpressionFactory

      public javax.el.ExpressionFactory getEagerExpressionFactory()
      Returns:
      The EL factory used to eagerly process expressions in templates by this instance.
    • getGlobalConfig

      public JinjavaConfig getGlobalConfig()
      Returns:
      The global config used as a base for all render operations performed by this instance.
    • getGlobalContext

      public Context getGlobalContext()
      The global render context includes such things as the base set of tags, filters, exp tests and functions, used as a base by all render operations performed by this instance
      Returns:
      the global render context
    • getGlobalContextCopy

      public Context getGlobalContextCopy()
    • getResourceLocator

      public ResourceLocator getResourceLocator()
    • getJinjavaDoc

      public JinjavaDoc getJinjavaDoc()
      Returns:
      a comprehensive descriptor of all available filters, functions, and tags registered on this jinjava instance.
    • getJinjavaSnippetDoc

      public String getJinjavaSnippetDoc()
      Returns:
      code snippets of all available filters, functions, and tags registered on this jinjava instance.
    • render

      public String render(String template, Map<String,?> bindings)
      Render the given template using the given context bindings.
      Parameters:
      template - jinja source template
      bindings - map of objects to put into scope for this rendering action
      Returns:
      the rendered template
      Throws:
      InterpretException - if any syntax errors were encountered during rendering
    • renderForResult

      public RenderResult renderForResult(String template, Map<String,?> bindings)
      Render the given template using the given context bindings. This method returns some metadata about the render process, including any errors which may have been encountered such as unknown variables or syntax errors. This method will not throw any exceptions; it is up to the caller to inspect the renderResult.errors collection if necessary / desired.
      Parameters:
      template - jinja source template
      bindings - map of objects to put into scope for this rendering action
      Returns:
      result object containing rendered output, render context, and any encountered errors
    • renderForResult

      public RenderResult renderForResult(String template, Map<String,?> bindings, JinjavaConfig renderConfig)
      Render the given template using the given context bindings. This method returns some metadata about the render process, including any errors which may have been encountered such as unknown variables or syntax errors. This method will not throw any exceptions; it is up to the caller to inspect the renderResult.errors collection if necessary / desired.
      Parameters:
      template - jinja source template
      bindings - map of objects to put into scope for this rendering action
      renderConfig - used to override specific config values for this render operation
      Returns:
      result object containing rendered output, render context, and any encountered errors
    • newInterpreter

      public JinjavaInterpreter newInterpreter()
      Creates a new interpreter instance using the global context and global config
      Returns:
      a new interpreter instance
    • registerTag

      public void registerTag(Tag t)
    • registerFunction

      public void registerFunction(ELFunctionDefinition f)
    • registerFilter

      public void registerFilter(Filter f)
    • registerExpTest

      public void registerExpTest(ExpTest t)