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

Array interpolation #52

Open
klmdbase opened this issue Dec 7, 2020 · 1 comment
Open

Array interpolation #52

klmdbase opened this issue Dec 7, 2020 · 1 comment

Comments

@klmdbase
Copy link

klmdbase commented Dec 7, 2020

First of all, I like this approach. It is relatively simple to implement but it needs a fully fledged example (maybe) or more information about what it can really do in the docs. All in all, it really helps.
Now. My problem is that I have an array of objects and I want to iterate it in the doc. Can I do it? How can I do it?
This is how others do. They are wrapping the interpolated items inside a # marked element.
image
Thanks!

@bdlowery
Copy link

bdlowery commented Oct 15, 2024

No you can't. You can "hack" it by using the formatter function to do it, but it just creates a long string. You can't choose where the items in the loop go.

const formatters = {
  experienceFormatter: (experiences) => {
    return experiences
      .map((exp) => {
        const bulletPoints = exp.bulletpoints
          .map((bp) => `• ${bp.item}`)
          .join('\n');

        return `${exp.company}\t${exp.jobcity}, ${exp.jobstate}
${exp.role}\t${exp.jobstart}${exp.jobend}
${bulletPoints}`;
      })
      .join('\n\n');
  },
}

await mustaches.interpolate({
  source: 'your-template-document-id', 
  destination: 'your-destination-document-id',
  data,
  formatters,
});

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