Skip to main content

render()

The render() method compiles a string to an HTML email.

Signature

const { html, config } = await render(input, options)

Parameters

input
string
required
The HTML string to compile. Must include at least <style> @tailwind utilities; </style> inside the <head> for CSS to be output or inlined.
options
object
Maizzle configuration object, like you would use in config.js. See configuration for all available options.

Return value

The method returns an object with the following properties:
html
string
The compiled HTML email string
config
object
The Environment config that was computed for this render

Examples

Basic usage

app.js
import { render } from '@maizzle/framework'

const { html, config } = await render(`<!doctype html>
<html>
  <head>
    <style>
      @tailwind utilities;
    </style>
  </head>
  <body>
    <div class="p-4">Hello, world!</div>
  </body>
</html>`)

console.log(html)

With options

app.js
import { render } from '@maizzle/framework'
import tailwindcssPresetEmail from 'tailwindcss-preset-email'

const input = `<!doctype html>
<html>
  <head>
    <style>
      @tailwind utilities;
    </style>
  </head>
  <body>
    <div class="p-4 bg-blue-500 text-white">
      Hello, world!
    </div>
  </body>
</html>`

const { html } = await render(input, {
  css: {
    inline: true,
    purge: true,
    shorthand: true,
    tailwind: {
      presets: [tailwindcssPresetEmail],
      content: [
        {
          raw: input,
          extension: 'html'
        }
      ]
    }
  }
})

console.log(html)

With front matter

app.js
import { render } from '@maizzle/framework'

let input = `---
title: Hello, world!
preheader: This is a preheader
---

<!doctype html>
<html>
  <head>
    <style>
      @tailwind utilities;
    </style>
  </head>
  <body>
    <div class="p-4">
      {{ page.title }}
    </div>
  </body>
</html>`

const { html } = await render(input)

console.log(html)

With templating

app.js
import { render } from '@maizzle/framework'

let html = `---
title: Using Maizzle programmatically
---

<x-main>
  <div class="p-4">
    {{ page.title }}
  </div>
</x-main>`

const { html: output } = await render(html)

console.log(output)
Paths to layouts or components in your string must be relative to the location where you execute the script.
Component x- tags only work in Node.js and when the referenced files are available on disk.

Build docs developers (and LLMs) love