Expressions
Handlebars-like, curly brace expression syntax is supported, allowing you to access variables from your Environment config or from a Template's Front Matter:
---
title: Example
---
<x-main>
The title is: {{ page.title }}
You ran the `maizzle build {{ page.env }}` command.
</x-main>
Running maizzle build production
would render this HTML:
The title is: Example
You ran the `maizzle build production` command.
You may use basic JavaScript expressions within curly braces:
<x-main>
doctype is {{ page.doctype || 'not set' }}
this email {{ page.env === 'production' ? "is" : "isn't" }} production ready!
</x-main>
Running maizzle build
, we would get:
doctype is not set
this email isn't production ready!
Unescaping
By default, special characters are escaped when using two curly braces:
---
markup: '<strong>Bold</strong>'
---
<x-main>
{{ page.markup }}
<!-- Result: <strong>Bold<strong> -->
</x-main>
If you need to render values exactly as they are, use triple curly braces:
---
markup: '<strong>Bold</strong>'
---
<x-main>
{{{ page.markup }}}
<!-- Result: <strong>Bold</strong> -->
</x-main>
Ignoring
Other templating engines and many ESPs also use the {{ }}
syntax.
If you want to prevent expression compilation and actually render the curly braces so you can evaluate them at a later stage, you have several options:
Ignore inline
The Blade-inspired @{{ }}
syntax is useful for one-offs, where you need to ignore a single expression. The compiled email will render {{ }}
without the @
.
<x-main>
@{{ page.markup }}
<!-- Result: {{ page.markup }} -->
</x-main>
Ignore in Front Matter
You may also use @{{ }}
to prevent expressions in Front Matter from being evaluated.
---
title: "Weekly newsletter no. @{{ edition_count }}"
---
<x-main>
{{ page.title }}
</x-main>
Result:
Weekly newsletter no. {{ edition_count }}
Ignore with <raw>
This is useful if you want to ignore multiple expressions in one go:
<raw>
<p>The quick brown {{ 1 + 2 }} jumps over the lazy {{ 3 + 4 }}.</p>
</raw>
<raw>
will be removed in the final output, but the curly braces will be left untouched:
<p>The quick brown {{ 1 + 2 }} jumps over the lazy {{ 3 + 4 }}.</p>
Change delimiters
You can change the delimiters to something else, like [[ ]]
:
module.exports = {
build: {
posthtml: {
expressions: {
delimiters: ['[[', ']]'],
unescapeDelimiters: ['[[[', ']]]']
}
}
}
}
Then you can safely use {{ }}
and its contents will not be evaluated:
<x-main>
<!-- This will be evaluated -->
[[ page.title ]]
<!-- But this won't be -->
Hi, {{ user.name }}.
</x-main>