Use expressions in Maizzle Markdown files
Maizzle supports the use of expressions within Markdown files. You can use them exactly as you would in any standard Maizzle template (Handlebars).
website·Indexed 33 days ago
https://v5.maizzle.com/docs/introductionA framework for creating email newsletters, featuring integration with Tailwind CSS, support for components, and capabilities for fetching and formatting external data such as RSS feeds via rss-parser.
<fetch> tag to retrieve JSON data from an external API endpoint directly within a Maizzle template. The retrieved data is available in the response array. You can access specific items using index notation (e.g., response[0]) to manually map API fields to your HTML markup.<x-main>
<fetch url="https://css-tricks.com/wp-json/wp/v2/posts?page=1&per_page=6&_embed=1">
<tr>
<td>
<h1>{{ response[0].title.rendered }}</h1>
<div>{{ response[0].excerpt.rendered }}</div>
<a href="{{ response[0].link }}">Read more</a>
</td>
</tr>
</fetch>
</x-main><fetch> tag, use the <each> tag with the loop attribute. You can also use JavaScript expressions within the loop attribute (such as .slice()) to iterate over a specific subset of the response data. The loop.last meta property can be used to conditionally render elements (like dividers) only after the final item.<fetch url="https://api.example.com/posts">
<each loop="post in response.slice(-2)">
<tr>
<td>
<h4><a href="{{ post.link }}">{{ post.title.rendered }}</a></h4>
<if condition="loop.last">
<hr />
</if>
</td>
</tr>
</each>
</fetch><x-alert>
# {{ page.title }}
This is the first newsletter.
</x-alert>{} immediately following the heading text. Classes must include a leading dot (.) and be separated by spaces. These classes are processed and inlined into the final HTML output.# Hello world {.m-0 .mb-10 .text-slate-900}highlight method for markdown-it within the beforeCreate event in your config.js. This ensures the highlighter is initialized once before templates are compiled.npm install shiki// config.js
import { createHighlighter } from 'shiki'
export default {
async beforeCreate(config) {
const highlighter = await createHighlighter({
themes: ['nord'],
langs: ['html'],
})
config = Object.assign(config, {
markdown: {
markdownit: {
highlight: (code, lang) => {
lang = lang || 'html'
return highlighter.codeToHtml(code, { lang, theme: 'nord' })
}
}
}
})
},
}prismjs library and configure the markdownit highlight function in config.js to use Prism's highlighting engine.npm i prismjs// config.js
const Prism = require('prismjs')
module.exports = {
markdown: {
markdownit: {
highlight(code, lang) {
lang = lang || 'markup'
return Prism.highlight(code, Prism.languages[lang], lang)
}
}
}
}layout property from the file's Front Matter using the front-matter package within the beforeRender hook of config.js.import fm from 'front-matter'
export default {
beforeRender(html) {
const { attributes, body } = fm(html)
const layout = attributes.layout || 'main'
return `
<x-${layout}>
<md>${body}</md>
</x-${layout}>`
}
}posthtml-prism plugin. This allows you to use standard <pre><code class="language-xxx"> markup, which the plugin then transforms into highlighted HTML during the build process.npm i posthtml-prism// config.js
module.exports = {
build: {
posthtml: {
plugins: [
require('posthtml-prism')()
]
}
}
}<!-- emails/example.html -->
<pre>
<code class="language-javascript">
function foo(bar) {
var a = 42,
b = 'Prism';
return a + bar(b);
}
</code>
</pre>rss-parser library within Maizzle's beforeCreate event to fetch RSS feed data and make it available to your templates. You can define custom fields to capture data not returned by default (e.g., subtitle or summary).npm install rss-parser// config.js
import Parser from 'rss-parser'
export default {
feed: {
url: 'https://laracasts.com/feed'
},
async beforeCreate(config) {
const parser = new Parser({
customFields: {
feed: ['subtitle'],
item: ['summary']
}
})
let feed = await parser.parseURL(config.feed.url)
config.feed = {
title: feed.title,
subtitle: feed.subtitle,
link: feed.link,
updated_at: feed.lastBuildDate,
posts: feed.items
}
}
}prism-synthwave84.css) to the css directory of your project and import it into your css/tailwind.css file between the components and utilities imports./* Tailwind CSS components */
@import "tailwindcss/components";
@import "custom/prism-synthwave84";
/* Tailwind CSS utility classes */
@import "tailwindcss/utilities";
/* Your custom utility classes */
@import "utilities";<fetch> tag in a Maizzle template to retrieve data from the WordPress REST API. You can use query parameters like per_page to limit results and _embed=1 to include additional fields such as featured media (_embedded["wp:featuredmedia"]). The fetched data is available within the template via the {{ response }} variable.<x-main>
<fetch url="https://css-tricks.com/wp-json/wp/v2/posts?page=1&per_page=6&_embed=1">
<!-- Posts are now available in {{ response }} -->
</fetch>
</x-main>