How to loop over arrays provided after build time? #1236
Replies: 15 comments
-
Essentially I need the each loop to be dynamic. Its impossible to know how many items are in the order before the order it placed. The built HTML needs to be able to render Listing order items must be an extremely common practice. I just cant find where in the documentation it explains how to do it. |
Beta Was this translation helpful? Give feedback.
-
Help me understand, why the
https://maizzle.com/docs/expressions#ignoring You'd need to pass an actual array/object data to the |
Beta Was this translation helpful? Give feedback.
-
@cossssmin I am ignoring the maizzle interpreted handlebars on purpose. The handlebars need to be present in the final built output because the data is dynamic for each order. The html needs to be hydrated dynamically. Is the solution to run |
Beta Was this translation helpful? Give feedback.
-
@cossssmin I guess I may be misunderstanding the correct way to use maizzle. How am I supposed to be generating dynamic emails based on user interactions? Currently, the firebase extension flow is as follows: export type MailData = {
to?: string | string[]
from?: string
replyTo?: string
message?: Message
template?: {
name: string
data?: { [key: string]: any }
}
toUids?: string[]
cc?: string[]
ccUids?: string[]
bcc?: string[]
bccUids?: string[]
headers?: any
}
type Message = {
subject?: string
text?: string
html?: string
messageId?: string
amp?: string
attachments?: Attachment[]
} That then gets queued for sending. If a template is used instead of message, then the data object in the template is injected into handlebars. What I am doing, is copying the build outputs from maizzle, and pasting them into the templates. That is why I need the handlebars |
Beta Was this translation helpful? Give feedback.
-
Have a look at using Maizzle programmatically: You'd pass your const Maizzle = require('@maizzle/framework')
const options = {
maizzle: {
locals: {
order: template.data
}
},
}
Maizzle
.render(`html string`, options)
.then(({html}) => console.log(html)) With that setup you can loop over <each loop="item, index in order">...</each> Mind the Tailwind config caveat, you need to pass it otherwise you'll get the default Tailwind config which doesn't play nice in emails. You can just read our const options = {
maizzle: {
locals: {
order: template.data
}
},
tailwind: {
config: JSON.parse(fs.readFileSync('path/to/tailwind.config.js'))
}
} |
Beta Was this translation helpful? Give feedback.
-
Where would this code be?
I dont have a "backend" in this stack. Firebase abstracts that away. It would seem slightly insane to bundle the entire maizzle templates directory with the front end and using the maizzle cli to build the html. |
Beta Was this translation helpful? Give feedback.
-
@cossssmin is If I were to completely bypass the build step, and instead use the file in the |
Beta Was this translation helpful? Give feedback.
-
Yeah to use it programmatically you need a Node backend, no other way around it. If you're just doing You could probably use the This kind of scenario is exactly what the API/programmatic is for - get the data, render the template with it. |
Beta Was this translation helpful? Give feedback.
-
The |
Beta Was this translation helpful? Give feedback.
-
@cossssmin
|
Beta Was this translation helpful? Give feedback.
-
It would but you're doing the work twice. If you have the variables like Flow would be like:
|
Beta Was this translation helpful? Give feedback.
-
@cossssmin I see, however I'm still not sure how |
Beta Was this translation helpful? Give feedback.
-
Yes, for the components right now only file system is supported, meaning that it scans them on disk. So in the So you'd just need the component files, not the entire project. Btw it's 172MB probably because of node_modules, which you'd ignore anyway. In the future we're looking into adding support for defining components right in the component config, so instead of scanning the file system it would use them from the config object - this will make it usable in more scenarios, including the browser. But it will take time, right now there's like one person working on this - me. |
Beta Was this translation helpful? Give feedback.
-
If your env doesn't support the file system, another idea would be to just inline the components. Especially if you're only using stuff like |
Beta Was this translation helpful? Give feedback.
-
@cossssmin I appreciate your work. I would be happy to support you by purchasing your templates when it becomes available. I love maizzle for the fact that the html is email client specific, and I can create emails with components. I would however like the option to inject variables into the full html string myself, using golang html/template or whatever html parser I choose. being confined to nodejs isnt ideal- I simply hate server side javascript and avoid it like the plague. I love maizzle and have been using it for a few years now. This is the first time I needed to iterate over an array of objects in an email with maizzle templates. |
Beta Was this translation helpful? Give feedback.
-
Is it possible to have each loops that are not strictly defined at build time?
I am using the templates along with firebase trigger email extension which allows me to pass data to the template via handlebars.
I am trying to send order confirmation emails with a list of all the order items:
The
@{{orderItems}}
variable is provided to the extension (which uses nodemailer) inside of a object nameddata
.I am able to display everything else- including the payment info, the date, the name and any other attribute I include in the
data
object, and reference in the template via@{{ propertyName }}
.The issue is that when I build the templates, everything inside of the each tag simply disappears...
Is this because the maizzle framework cant evaluate the
@{{orderItems}}
array at build time and defaults to ignoring the loop entirely?Surely this is a common issue? How do other people send order confirmation emails or receipts with a list of items that the customer purchased.
Beta Was this translation helpful? Give feedback.
All reactions