DEV Community

Cover image for Dynamic sendgrid template for multiple items!
sonu sharma
sonu sharma

Posted on

8 2

Dynamic sendgrid template for multiple items!

Ever wondered how to add multiple items in sendgrid template ?
For example you need to notify your platform users about list of courses available. Since the number of courses are dynamic you cannot create a static template for it.
Lets take an example of Grocery list template
sendgrid template

The list of items is dynamic.



{
   "items" : [
      {
        "name" : "onion",
        "quantity": 5,
      },
      {
        "name" : "potato",
        "quantity": 5,
      },
      {
        "name" : "chilli",
        "quantity": 5,
      }

   ]
}


Enter fullscreen mode Exit fullscreen mode

Inorder to iterate over the items of the nested array, sendgrid gives handlebar capbilities. Example code



{{#if items}}
  <h2 style="margin:10px 10px">Grocery items !</h2>
  {{#each items}}
    <div style="border:1px solid black;min-width:300px;min-height:50px;padding:10px 10px;margin:10px 10px">
        Got to buy {{this.name}}
    </div>

  {{/each}}
{{/if}}


Enter fullscreen mode Exit fullscreen mode

You can include the similar code to iterate over items and produce a good marketing email templates.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay