Usage
You may use Events both when developing locally with the CLIbuild or serve commands, and when using the API with the render() method.
CLI
To use events with the CLI commands, add them to yourconfig.js file:
config.js
API Events
When using the API, add events inside the object that you pass to therender() method:
app.js
Event types
These are the Events that you can use in Maizzle. The following ones are CLI-only - they run only when added inside theevents: {} object in your config.js and when you run one of the build commands:
These always run, every time a Template is compiled:
beforeCreate
Runs after the Environment config has been computed, but before Templates are processed. Exposes the config object so you can further customize it. For example, let’s use a custom highlight function for Markdown fenced code blocks:config.js
Use
beforeCreate if you need to update the config only once.beforeRender
Runs after the Template’s config has been computed, but just before it is compiled. It exposes the Template’s HTML and Front Matter, as well as its config. For (a silly) example, let’s fetch data from an API and set it as the preheader text:config.js
layouts/main.html
beforeRender runs for each Template that is going to be compiled. For performance reasons, you should only use it if you need access to the config of the Template that is about to be compiled (which includes variables from the Template’s Front Matter).
afterRender
Runs after the Template has been compiled, but before any Transformers have been applied. Exposes the renderedhtml string and the config, as well as the Template’s Front Matter.
It’s your last chance to alter the HTML or any settings in your config, before Transformers process your email template.
For example, let’s disable CSS inlining:
config.js
afterRender runs for each Template, right after it has been compiled. Use it only if you need access to the config of the Template that was just compiled.
afterTransformers
Runs after all Transformers have been applied, just before the final HTML is returned. It exposes the same options asafterRender(), so you can do further adjustments to the HTML, or read some config settings.
For example, maybe you don’t like the minifier that Maizzle includes, and you disabled it in your config so that you can use your own:
config.js
afterBuild
Runs after all Templates have been compiled and output to disk. Thefiles parameter will contain the paths to all the files inside the build.output.path directory.
config.js
maizzle build production will output: