Class MjmlRenderer
java.lang.Object
dev.jcputney.mjml.MjmlRenderer
Public API entry point for rendering MJML templates to HTML.
Thread safety: Both the static render() methods and the instance API
are thread-safe. Each render call creates its own GlobalContext, so concurrent calls do not share mutable state. The
provided MjmlConfiguration is immutable and safe to share.
Usage
// One-liner with defaults (convenience, creates a new pipeline each time)
MjmlRenderResult result = MjmlRenderer.render(mjmlString);
String html = result.html();
// Instance API (preferred for repeated rendering — reuses the pipeline)
MjmlRenderer renderer = MjmlRenderer.create(config);
MjmlRenderResult r1 = renderer.renderTemplate(mjml1);
MjmlRenderResult r2 = renderer.renderTemplate(mjml2);
-
Method Summary
Modifier and TypeMethodDescriptionstatic MjmlRenderercreate()Creates a reusable renderer instance with default configuration.static MjmlRenderercreate(MjmlConfiguration configuration) Creates a reusable renderer instance with the given configuration.static MjmlRenderResultRenders an MJML template to HTML using default configuration.static MjmlRenderResultrender(String mjml, MjmlConfiguration configuration) Renders an MJML template to HTML with the given configuration.static MjmlRenderResultRenders an MJML file to HTML using default configuration.static MjmlRenderResultrender(Path mjmlFile, MjmlConfiguration configuration) Renders an MJML file to HTML with the given configuration.renderTemplate(String mjml) Renders an MJML template to HTML using this instance's configuration.renderTemplate(String mjml, IncludeResolver resolver) Renders an MJML template to HTML with a specific include resolver, overriding the instance's configured resolver for this call only.renderTemplate(Path mjmlFile) Renders an MJML file to HTML using this instance's configuration.
-
Method Details
-
create
Creates a reusable renderer instance with the given configuration. The instance caches and reuses the internalRenderPipelineandComponentRegistry, making it significantly faster for repeated rendering than the static convenience methods.This is the preferred API when rendering multiple templates with the same configuration.
- Parameters:
configuration- the rendering configuration- Returns:
- a reusable renderer instance
-
create
Creates a reusable renderer instance with default configuration.- Returns:
- a reusable renderer instance
-
render
Renders an MJML template to HTML using default configuration.For repeated rendering, prefer the instance API via
create(MjmlConfiguration).- Parameters:
mjml- the MJML source string- Returns:
- the render result containing HTML and metadata
- Throws:
MjmlException- if parsing or rendering fails
-
render
Renders an MJML template to HTML with the given configuration.For repeated rendering, prefer the instance API via
create(MjmlConfiguration).- Parameters:
mjml- the MJML source stringconfiguration- the rendering configuration- Returns:
- the render result containing HTML and metadata
- Throws:
MjmlException- if parsing or rendering fails
-
render
Renders an MJML file to HTML using default configuration. Automatically configures a file system include resolver using the file's parent directory.For repeated rendering, prefer the instance API via
create(MjmlConfiguration).- Parameters:
mjmlFile- path to the MJML file- Returns:
- the render result containing HTML and metadata
- Throws:
MjmlException- if reading, parsing, or rendering fails
-
render
Renders an MJML file to HTML with the given configuration. If no include resolver is configured, one is automatically created using the file's parent directory.For repeated rendering, prefer the instance API via
create(MjmlConfiguration).- Parameters:
mjmlFile- path to the MJML fileconfiguration- the rendering configuration- Returns:
- the render result containing HTML and metadata
- Throws:
MjmlException- if reading, parsing, or rendering fails
-
renderTemplate
Renders an MJML template to HTML using this instance's configuration.- Parameters:
mjml- the MJML source string- Returns:
- the render result containing HTML and metadata
- Throws:
MjmlException- if parsing or rendering fails
-
renderTemplate
Renders an MJML file to HTML using this instance's configuration. If no include resolver is configured, one is automatically created using the file's parent directory.- Parameters:
mjmlFile- path to the MJML file- Returns:
- the render result containing HTML and metadata
- Throws:
MjmlException- if reading, parsing, or rendering fails
-
renderTemplate
Renders an MJML template to HTML with a specific include resolver, overriding the instance's configured resolver for this call only.- Parameters:
mjml- the MJML source stringresolver- the include resolver to use- Returns:
- the render result containing HTML and metadata
- Throws:
MjmlException- if parsing or rendering fails
-