Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loop over object not working #916

Closed
lowbits opened this issue Feb 22, 2023 · 2 comments
Closed

Loop over object not working #916

lowbits opened this issue Feb 22, 2023 · 2 comments

Comments

@lowbits
Copy link

lowbits commented Feb 22, 2023

  • Maizzle Version: 4.3.1
  • Node.js Version: v18.13.0

Im trying to loop over on object like {annual_revenue: 3000, employees: 3}, but when i do

<each loop="key, value in {annual_revenue: 3000, employees: 3}">
   <p class="mt-2 mb-3 text-slate-500">{{ key }}: {{ value }}</p>
</each>

The generated HTML is empty, when I loop over an array it is working.

But is it also possible to loop over a parameter coming from context, for example, with handlebars .hbs?

@cossssmin
Copy link
Member

Can confirm, it won't work if you pass the object inline. Looks like this is actually how posthtml-expressions currently works, I've opened a PR to replicate the issue and see what could be done about it: posthtml/posthtml-expressions#146

In Maizzle you can either set that data in the config.js or in the template's Front Matter, and use it off the page object.

Here's how you'd do it with FM:

---
obj:
  annual_revenue: 3000
  employees: 3
---

<each loop='key, value in page.obj'>
  {{ key }}: {{ value }}
</each>

You'd use page.obj the same way if you added the data to config.js:

module.exports = {
  obj: {
    annual_revenue: 3000,
    employees: 3
  },
  build: { ... },
}

@lowbits
Copy link
Author

lowbits commented Mar 8, 2023

Thank you for your reply, currently, I adjust this in the created .hbs.
So every time I adjust the template, in maizzle I need to adjust it. For now, it's okay.

@lowbits lowbits closed this as completed Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants