Package com.hubspot.jinjava
Class Jinjava
java.lang.Object
com.hubspot.jinjava.Jinjava
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 Summary
ConstructorsConstructorDescriptionJinjava()Create a new Jinjava processor instance with the default global configJinjava(JinjavaConfig globalConfig) Create a new jinjava processor instance with the specified global config -
Method Summary
Modifier and TypeMethodDescriptionjavax.el.ExpressionFactoryjavax.el.ExpressionFactoryThe 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 instanceCreates a new interpreter instance using the global context and global configvoidvoidvoidvoidregisterTag(Tag t) Render the given template using the given context bindings.renderForResult(String template, Map<String, ?> bindings) Render the given template using the given context bindings.renderForResult(String template, Map<String, ?> bindings, JinjavaConfig renderConfig) Render the given template using the given context bindings.voidsetResourceLocator(ResourceLocator resourceLocator) Set the object responsible for locating templates referenced in other templates
-
Constructor Details
-
Jinjava
public Jinjava()Create a new Jinjava processor instance with the default global config -
Jinjava
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
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
- Returns:
- The global config used as a base for all render operations performed by this instance.
-
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
-
getResourceLocator
-
getJinjavaDoc
- Returns:
- a comprehensive descriptor of all available filters, functions, and tags registered on this jinjava instance.
-
getJinjavaSnippetDoc
- Returns:
- code snippets of all available filters, functions, and tags registered on this jinjava instance.
-
render
Render the given template using the given context bindings.- Parameters:
template- jinja source templatebindings- 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
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 templatebindings- 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 templatebindings- map of objects to put into scope for this rendering actionrenderConfig- used to override specific config values for this render operation- Returns:
- result object containing rendered output, render context, and any encountered errors
-
newInterpreter
Creates a new interpreter instance using the global context and global config- Returns:
- a new interpreter instance
-
registerTag
-
registerFunction
-
registerFilter
-
registerExpTest
-